Py之pytest-shutil:Python库之pytest-shutil简介、安装、使用方法之详细攻略

举报
一个处女座的程序猿 发表于 2021/03/27 01:24:51 2021/03/27
【摘要】 Py之pytest-shutil:Python库之pytest-shutil简介、安装、使用方法之详细攻略     目录 pytest-shutil简介 pytest-shutil安装 pytest-shutil函数使用 pytest-shutil使用方法       pytest-shutil简介 ...

Py之pytest-shutil:Python库之pytest-shutil简介、安装、使用方法之详细攻略

 

 

目录

pytest-shutil简介

pytest-shutil安装

pytest-shutil函数使用

pytest-shutil使用方法


 

 

 

pytest-shutil简介

    这个库是一个很好的用于自动测试的Unix shell和环境管理工具包。下面是可用函数的摘要,请查看完整列表的源代码。

 


 

pytest-shutil安装

pip install pytest-shutil
 

 

 

pytest-shutil函数使用

import shutil

copy()


  
  1. 功能:复制文件
  2. 格式:shutil.copy('来源文件','目标地址')
  3. 返回值:复制之后的路径

copy2()


  
  1. 功能:复制文件,保留元数据
  2. 格式:shutil.copy2('来源文件','目标地址')
  3. 返回值:复制之后的路径

copyfileobj()


  
  1. 将一个文件的内容拷贝的另外一个文件当中
  2. 格式:shutil.copyfileobj(open(来源文件,'r'),open'目标文件','w'))
  3. 返回值:无

copyfile()


  
  1. 功能:将一个文件的内容拷贝的另外一个文件当中
  2. 格式:shutil.copyfile(来源文件,目标文件)
  3. 返回值:目标文件的路径

copytree()


  
  1. 功能:复制整个文件目录
  2. 格式:shutil.copytree(来源目录,目标目录)
  3. 返回值:目标目录的路径
  4. 注意:无论文件夹是否为空,均可以复制,而且会复制文件夹中的所有内容

copymode()

功能:拷贝权限

 

copystat()

功能:拷贝元数据(状态)

 

rmtree()


  
  1. 功能:移除整个目录,无论是否空
  2. 格式:shutil.rmtree(目录路径)
  3. 返回值:无

move()


  
  1. 功能:移动文件或者文件夹
  2. 格式:shutil.move(来源地址,目标地址)
  3. 返回值:目标地址

which()


  
  1. 功能:检测命令对应的文件路径
  2. 格式:shutil.which(‘命令字符串’)
  3. 返回值:命令文件所在位置
  4. 注意:window和linux不太一样。 window的命令都是.exe结尾,linux则不是

disk_usage()


  
  1. 功能:检测磁盘使用信息
  2. 格式:disk_usage(‘盘符’)
  3. 返回值:元组

 

 

 

pytest-shutil使用方法


  
  1. def test_something(workspace):
  2. # Workspaces contain a handle to the path.py path object (see https://pythonhosted.org/path.py) path = workspace.workspace script = path / ‘hello.sh’ script.write_text(‘#!/bin/shn echo hello world!’)
  3. # There is a ‘run’ method to execute things relative to the workspace root workspace.run(‘hello.sh’)

1、pytest_shutil.env: Shell helpers

function description
set_env contextmanager to set env vars
unset_env contextmanager to unset env vars
no_env contextmanager to unset a single env var
no_cov contextmanager to disable coverage in subprocesses

2、pytest_shutil.cmdline: Command-line helpers

function description
umask contextmanager to set the umask
chdir contextmanager to change to a directory
TempDir contextmanager for a temporary directory
PrettyFormatter simple text formatter for drawing title, paragrahs, hrs.
copy_files copy all files from one directory to another
getch cross-platform read of a single character from the screen
which analoge of unix which
get_real_python_executable find our system Python, useful when running under virtualenv

3、pytest_shutil.run: Running things in subprocesses

function description
run run a command, with options for capturing output, checking return codes.
run_as_main run a function as if it was the system entry point
run_module_as_main run a module as if it was the system entry point
run_in_subprocess run a function in a subprocess
run_with_coverage run a command with coverage enabled

 

 

文章来源: yunyaniu.blog.csdn.net,作者:一个处女座的程序猿,版权归原作者所有,如需转载,请联系作者。

原文链接:yunyaniu.blog.csdn.net/article/details/80472368

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。