Python使用shutil操作文件、subprocess运行子程序
作者:springsnow 发布时间:2021-03-01 00:19:19
标签:Python,shutil,操作,文件,subprocess,运行,子程序
一、shutil模块(了解):高级的文件、文件夹、压缩包处理模块。
import shutil
# shutil.copyfileobj(fsrc, fdst[, length]),将文件内容拷贝到另一个文件中
shutil.copyfileobj(open('old.xml', 'r'), open('new.xml', 'w'))
# shutil.copyfile(src, dst),拷贝文件
shutil.copyfile('f1.log', 'f2.log') # 目标文件无需存在
# shutil.copymode(src, dst),仅拷贝权限。内容、组、用户均不变
shutil.copymode('f1.log', 'f2.log') # 目标文件必须存在
# shutil.copystat(src, dst),仅拷贝状态的信息,包括:mode bits, atime, mtime, flags
shutil.copystat('f1.log', 'f2.log') # 目标文件必须存在
# shutil.copy(src, dst),拷贝文件和权限
shutil.copy('f1.log', 'f2.log')
# shutil.copy2(src, dst),拷贝文件和状态信息
shutil.copy2('f1.log', 'f2.log')
# shutil.ignore_patterns(*patterns)
# shutil.copytree(src, dst, symlinks=False, ignore=None),递归的去拷贝文件夹
# 目标目录不能存在,注意对folder2目录父级目录要有可写权限,ignore的意思是排除
shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
# shutil.rmtree(path[, ignore_errors[, onerror]]),递归的去删除文件
shutil.rmtree('folder1')
# shutil.move(src, dst),递归的去移动文件,它类似mv命令,其实就是重命名
shutil.move('folder1', 'folder3')
# shutil.make_archive(base_name, format, ...),创建压缩包并返回文件路径,例如:zip、tar
'''
base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,如 data_bak = >保存至当前路径;/ tmp/data_bak = >保存至/tmp/
format:压缩包种类,“zip”, “tar”, “bztar”,“gztar”
root_dir:要压缩的文件夹路径(默认当前目录)
owner:用户,默认当前用户
group:组,默认当前组
logger:用于记录日志,通常是logging.Logger对象
'''
# 将 /data 下的文件打包放置当前程序目录
ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data')
# 将 /data下的文件打包放置 /tmp/目录
ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data')
shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的,详细:
1、 zipfile压缩解压缩
import zipfile
# 压缩
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()
# 解压
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall(path='.')
z.close()
2、 tarfile压缩解压缩
import tarfile
# 压缩
t=tarfile.open('/tmp/egon.tar','w')
t.add('/test1/a.py',arcname='a.bak')
t.add('/test1/b.py',arcname='b.bak')
t.close()
# 解压
t=tarfile.open('/tmp/egon.tar','r')
t.extractall('/egon')
t.close()
二、subprocess模块(了解):运行子程序
subprocess模块允许你去创建一个新的进程让其执行另外的程序,并与它进行通信,获取标准的输入、标准输出、标准错误以及返回码等。
更多查看官网:https://docs.python.org/2/library/subprocess.html?highlight=subprocess#frequently-used-arguments
import subprocess
'''
sh-3.2# ls /Users/nick/Desktop |grep txt$
mysql.txt
tt.txt
事物.txt
'''
res1 = subprocess.Popen('ls /Users/jieli/Desktop',shell=True, stdout=subprocess.PIPE)
res = subprocess.Popen('grep txt$', shell=True,stdin=res1.stdout, stdout=subprocess.PIPE)
print(res.stdout.read().decode('utf-8'))
# 下面这段等同于上面,但是上面的优势在于,一个数据流可以和另外一个数据流交互,可以通过爬虫得到结果然后交给grep。
res1 = subprocess.Popen('ls /Users/jieli/Desktop |grep txt$',shell=True, stdout=subprocess.PIPE)
print(res1.stdout.read().decode('utf-8'))
# windows下:
# dir | findstr 'test*'
# dir | findstr 'txt$'
res1 = subprocess.Popen(r'dir “C:\Users\Administrator\PycharmProjects\test\函数备课”', shell=True, stdout=subprocess.PIPE)
res = subprocess.Popen('findstr test*', shell=True, stdin=res1.stdout, stdout=subprocess.PIPE)
# subprocess使用当前系统默认编码,得到结果为bytes类型,在windows下需要用gbk解码
print(res.stdout.read().decode('gbk') )
来源:https://www.cnblogs.com/springsnow/p/11974749.html
0
投稿
猜你喜欢
- Python 定义了类型转换函数以将一种数据类型直接转换为另一种数据类型,这在日常和竞争性编程中很有用。本文旨在提供有关某些转换函数的信息。
- JMeter可以通过os命令调用Python脚本,Python同样可以通过系统命令调用JMeter执行压测Python调用JMeter首先要
- Python下一切皆对象,每个对象都有多个属性(attribute),Python对属性有一套统一的管理方案。__dict__与dir()的
- I/O吞吐量小,形成了瓶颈效应。 没有创建计算列导致查询不优化。 内存不足。 网络速度慢。 查询出的数据量过大(可以采用多次查询,其他的方法
- 在 go 的标准库中,提供了 sync.Cond 这个并发原语,让我们可以实现多个 goroutine 等待某一条件满足之后再继续执行。 它
- 最近出现一个很奇怪的MySQL问题,使用不同select语句查询全部数据集居然得到不同的记录数。select * 得到4条记录,select
- 前言:MySQL 有很多存储引擎(也叫数据引擎),所谓的存储引擎是指用于存储、处理和保护数据的核心服务。也就是存储引擎是数据库的底层软件组织
- DOM遍历基于ID、元素类型、类名查找元素非常有用,但是如果你想基于它在DOM树中的位置来查找元素该怎么办?换句话说,你有一个给定的元素,你
- 前言:本文的主要内容是介绍Python中的变量命名规则和简单数据类型的应用,简单的数据类型包括字符串和数字等,文中还附有代码以及相应的运行结
- 404页面对于站长来说应该并不陌生,其作用无碍乎二点:提高用户体验和增强对搜索引擎的友好性。去年在跟几个朋友在聊天的时候,跟我说404页面不
- 从2015开始国内就开始慢慢接触Python了,从16年开始Python就已经在国内的热度更高了,目前也可以算的上"全民Pytho
- Django Model的外键自关联在django的model定义中,有时需要某个Field引用当前定义的Model,比如一个部门(Depa
- 导语:通过node_exporter监控GPU以及cpu频率、温度,不想用一个node_exporter再加一个dcgm,分开监控。我这里监
- 发现问题项目需要,需要删除文件夹中的冗余图片。涉及图像文件名的操作,图像文件名存储在list中python list删除元素有remove(
- 前言嗨喽!大家好呀,这里是魔王~雪球,聪明的投资者都在这里 - 4300万投资者都在用的投资社区,沪深港美全球市场实时行情,股票基金债券免费
- 奥运来了,三大门户网站都加上了奥运主题。加上下面代码你的网站也拥有奥运主题了:<style type="text/
- Anaconda简介Anaconda,是一个开源的Python发行版本,其包含了conda、Python以及一大堆安装好的工具包及依赖项,比
- 本例详细介绍了如何在wiondws XP下安装与配置MySQL5.0.37 ,图文并茂,相信对初学mysql的朋友有所帮助。1 点击MySQ
- python3 判断空列表@(python3)有个判断列表是否为空的需求,试了好多方式,比如:a = []if a is not None:
- 前言近几日迫于被辅导员三番五次的提醒每日一报打卡,就想着去写个脚本挂在服务器上定时执行。经过我不懈的努力,最终选择了seleniumsele