日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

Python使用shutil操作文件、subprocess運行子程序_python

作者:springsnow ? 更新時間: 2022-07-26 編程語言

一、shutil模塊(了解):高級的文件、文件夾、壓縮包處理模塊。

import shutil

# shutil.copyfileobj(fsrc, fdst[, length]),將文件內(nèi)容拷貝到另一個文件中
shutil.copyfileobj(open('old.xml', 'r'), open('new.xml', 'w'))

# shutil.copyfile(src, dst),拷貝文件
shutil.copyfile('f1.log', 'f2.log')  # 目標文件無需存在

# shutil.copymode(src, dst),僅拷貝權(quán)限。內(nèi)容、組、用戶均不變
shutil.copymode('f1.log', 'f2.log')  # 目標文件必須存在

# shutil.copystat(src, dst),僅拷貝狀態(tài)的信息,包括:mode bits, atime, mtime, flags
shutil.copystat('f1.log', 'f2.log')  # 目標文件必須存在

# shutil.copy(src, dst),拷貝文件和權(quán)限
shutil.copy('f1.log', 'f2.log')

# shutil.copy2(src, dst),拷貝文件和狀態(tài)信息
shutil.copy2('f1.log', 'f2.log')

# shutil.ignore_patterns(*patterns)
# shutil.copytree(src, dst, symlinks=False, ignore=None),遞歸的去拷貝文件夾
# 目標目錄不能存在,注意對folder2目錄父級目錄要有可寫權(quán)限,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, ...),創(chuàng)建壓縮包并返回文件路徑,例如: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 對壓縮包的處理是調(diào)用 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模塊允許你去創(chuàng)建一個新的進程讓其執(zhí)行另外的程序,并與它進行通信,獲取標準的輸入、標準輸出、標準錯誤以及返回碼等。

更多查看官網(wǎng):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'))

# 下面這段等同于上面,但是上面的優(yōu)勢在于,一個數(shù)據(jù)流可以和另外一個數(shù)據(jù)流交互,可以通過爬蟲得到結(jié)果然后交給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\函數(shù)備課”',  shell=True, stdout=subprocess.PIPE)
res = subprocess.Popen('findstr test*', shell=True,  stdin=res1.stdout,   stdout=subprocess.PIPE)

# subprocess使用當前系統(tǒng)默認編碼,得到結(jié)果為bytes類型,在windows下需要用gbk解碼
print(res.stdout.read().decode('gbk') )

原文鏈接:https://www.cnblogs.com/springsnow/p/11974749.html

欄目分類
最近更新