shutil 模块
shutil(或称为 shell 工具)模块中包含一些函数,复制、移动、改名和删除文件。要使用 shutil 的函数,首先需要 import shutil。
>>> os.mkdir(os.path.join('E:\\','path','abc')) ## 只能建一级子目录
>>> os.mkdir(os.path.join('E:\\','path','abc','def')) ## 多层目录需一级一级建
>>> os.mkdir(os.path.join('E:\\','path','abc','def','123','456')) ## 建立多层会报错
Traceback (most recent call last):
File "", line 1, in
os.mkdir(os.path.join('E:\\','path','abc','def','123','456'))
FileNotFoundError: [WinError 3] 系统找不到指定的路径。: 'E:\\path\\abc\\def\\123\\456'
>>> os.makedirs(os.path.join('E:\\','path','x','y','z')) ## 建立N层目录结构
>>> os.chdir(os.path.join('E:\\','path')) ## 修改当前目录,接下来操作都在此目录中
>>> shutil.copy('note.txt','x') ## 将note.txt文件复制到x文件夹中
'x\\note.txt'
>>> shutil.copy('note.txt','x\\x.txt') ## 将note.txt复制到x文件夹中并重命名为x.txt
'x\\x.txt'
>>> shutil.copytree('x','abc') ## 复制整个文件夹时第二个参数必须为新目录
Traceback (most recent call last):
File "", line 1, in
shutil.copytree('x','abc')
File "D:\work\Python\Python36\lib\shutil.py", line 315, in copytree
os.makedirs(dst)
File "D:\work\Python\Python36\lib\os.py", line 220, in makedirs
mkdir(name, mode)
FileExistsError: [WinError 183] 当文件已存在时,无法创建该文件。: 'abc'
>>> shutil.copytree('x','test') ## 将x文件夹复制到test目录下,test目录不存在会新建
'test'
>>> shutil.move('note.txt','note') ## note文件夹不存在,此时会重命名为note文件(没有后缀名)
'note'
>>> shutil.move('abc','t') ## 将abc重命名为t文件夹(t文件夹不存在则重命名,t文件夹存在则移动到t文件内)
't'
>>> shutil.move('abc','x\\y') ## 存在 x\\y 文件夹则移动
'x\\y\\abc'
>>> shutil.move('x','abc') ## 不存在abc文件夹则重命名
'abc'
>>> shutil.move('abc','123') ## 存在 123 文件夹则移动
'123\\abc'
>>> shutil.move('123','x\\y\\z') ## 不存在x\\y\\z文件夹则重命名
'x\\y\\z'
>>> shutil.move('note','note.txt') ## 将note文件(没有后缀名)重命名为note.txt文件
'note.txt'
>>> shutil.move('note.txt','t') ## 移动文件到目录,若存在则报错
Traceback (most recent call last):
File "", line 1, in
shutil.move('note.txt','t')
File "D:\work\Python\Python36\lib\shutil.py", line 542, in move
raise Error("Destination path '%s' already exists" % real_dst)
shutil.Error: Destination path 't\note.txt' already exists
>>> shutil.move('x\\note.txt','x\\abc.txt') ## 将note.txt重命名为abc.txt
'x\\abc.txt'
>>> shutil.move('t','abc') ## 改回abc文件夹
'abc'
>>> os.unlink('abc\\note.txt') ## 删除单个文件,永久删除,不在回收站
>>> os.rmdir('abc\\def') ## 删除单个文件夹,文件夹内必须为空,永久删除,不在回收站
>>> shutil.rmtree('x') ## 删除整个文件夹,永久删除,不在回收站
>>> import send2trash
>>> send2trash.send2trash('test') ## 删除test文件夹,可在回收站恢复
>>> send2trash.send2trash('note.txt') ## 删除note.txt文件,可在回收站恢复
shutil.move 可以移动文件和文件夹,第二个参数有则移动,无则重命名(重命名包括移动且重命名)
遍历目录树
os.walk()函数传入一个路径。 for 循环语句中使用 os.walk()函数,遍历目录树, 就像使用 range()函数遍历一个范围的数字一样。os.walk()在循环的每次迭代中,返回 3 个值:
1.当前文件夹名称的字符串。
2.当前文件夹中子文件夹的字符串的列表。
3.当前文件夹中文件的字符串的列表。
所谓当前文件夹,是指 for 循环当前迭代的文件夹。程序的当前工作目录,不会因为 os.walk()而改变。
import os,send2trash
for folder,subfolders,filename in os.walk(os.path.join('E:\\','path')):
for file in filename:
if file!='note.txt' and file.endswith('.txt'):
send2trash.send2trash(os.path.join(folder,file))
用 tarfile 模块压缩文件
>>> file=tarfile.open(os.path.join('E:\\','path','中国.zip'),'w')
>>> file.add(os.path.join('E:\\','path','中国.txt'))
>>> file.close()
>>> os.chdir(os.path.join('E:\\','path'))
>>> file.add('中国.txt')
>>> file.close()
>>> file=tarfile.open('中国.7z','w')
>>> file.add('中国.txt')
>>> file.close()
## 将图片文件打包成图片.7z
import os,tarfile
os.chdir(os.path.join('E:\\','图片'))
file=tarfile.open(os.path.join('E:\\','图片.7z'),'w')
for folder,subfolders,filenames in os.walk(os.getcwd()):
for filename in filenames:
## os.path.relpath取相对路径,否则添加到压缩内会带有多余的文件夹层
file.add(os.path.join(os.path.relpath(folder,os.getcwd()),filename))
file.close()
## 读取图片.7z内容
>>> file=tarfile.open(os.path.join('E:\\','图片.7z'))
>>> type(file)
class 'tarfile.TarFile'
>>> file.getnames()
>>> file.getmembers()
>>> member=file.getmembers()
>>> info=member[0]
>>> info.name
'关之琳/1.jpg'
>>> info.size
213558
>>> info.mode
438
>>> info.type
b'0'
>>> file.close()
>>> file=tarfile.open(os.path.join('E:\\','path','图片.7z'))
>>> file.extractall() ## 解压整个包
>>> file.close()