shutil模块(文件,文件夹,压缩包处理)

时间:2022-04-27 22:20:47

shutil.copyfileobj(fsrc, fdst[, length]) # 将文件内容拷贝到另外一个文件中,可以部分内容

>>> with open('today.txt') as f:
...   print(f.read())
...
明天周一,
>>> with open('new.txt') as f:
...   print(f.read())
...
{"today": "\u661f\u671f\u516d", "mood": "good"}

>>> import shutil
>>> shutil.copyfileobj(open('today.txt', 'rb'),open('new.txt', 'ab'), 2) # 这里的2应该指的2kb
>>> with open('new.txt') as f:
...   print(f.read())
...
{"today": "\u661f\u671f\u516d", "mood": "good"}明天周一,
>>> with open('today.txt') as f:
...   print(f.read())
...
明天周一,

 

shutil.copyfile(src, dst) 拷贝文件

>>> shutil.copyfile('today.txt', 'copytoday.txt')
'copytoday.txt'
>>> with open('copytoday.txt') as f:
...   print(f.read())
...
明天周一,

 

shutil.copymode(src, dst) # 仅拷贝权限,内容,组,用户都不变

 

shutil.copystat(src, dst) # 拷贝状态的信息,包括:mode bits, atime, mtime, flags

 

shutil.copy(src, dst) # 拷贝文件和权限