shutil.copy() 复制一个文件,shutil.copytree()将复制整个文件夹
>>> import shutil, os >>> os.chdir('C:\\PyProjects') >>> shutil.copy('C:\\PyProjects\\this.txt','C:\\PyProjects\\this_copy.txt') 'C:\\PyProjects\\this_copy.txt' >>> shutil.copytree('bacon','bacon_copy') 'bacon_copy'
shutil.move(source, destination)
>>> import shutil >>> dir <built-in function dir> >>> import shutil >>> shutil.move('C:\\PyProjects\\bacon.txt','C:\\PyProjects\\bacon') 'C:\\PyProjects\\bacon\\bacon.txt'
改名
>>> shutil.move('C:\\PyProjects\\bacon\\bacon.txt','C:\\PyProjects\\bacon\\new_bacon.txt') 'C:\\PyProjects\\bacon\\new_bacon.txt'
利用os模块中的函数,可以删除一个文件或一个空文件夹,但利用shutil模块,可以删除一个文件夹及其所有的内容。
import os for filename in os.listdir('C:\\PyProjects'): if filename.endswith('.txt'): print(filename) #os.unlink(filename)
C:\PyProjects>C:/Python37/python3.exe c:/PyProjects/demo.py bacon.txt this.txt
安装send2trash
C:\Users\ElonTian>pip3 install send2trash Collecting send2trash Downloading Send2Trash-1.8.0-py3-none-any.whl (18 kB) Installing collected packages: send2trash Successfully installed send2trash-1.8.0 WARNING: You are using pip version 21.1.2; however, version 22.0.4 is available. You should consider upgrading via the 'c:\python37\python3.exe -m pip install --upgrade pip' command.
利用send2trash,比Python常规的删除函数要安全得多,因为它将文件夹和文件发送到计算机的垃圾箱或回收站,而不是永久删除。
>>> import send2trash >>> os.chdir('C:\\PyProjects') >>> baconFile = open('bacon.txt', 'a') >>> baconFile.write('Bacon is not a vegetable.') 25 >>> baconFile.close() >>> send2trash.send2trash('bacon.txt') >>>
send2trash.send2trash()函数删除文件和文件夹,虽然它将文件发送到垃圾箱,稍后能够恢复它们,但这不像永久删除文件,不会释放磁盘空间。这时,os和shutil可以删除文件和文件夹,释放磁盘空间。注意,send2trash()函数只能将文件送到垃圾箱,不能从中恢复文件。
os.walk() 函数被传入一个字符串值,即一个文件夹的路径。在一个for循环语句中使用os.walk()函数,遍历目录树。os.walk()在循环的每次迭代中,返回3个值:
import os for folderName, subfolders, filenames in os.walk('C:\\PyProjects'): print('The current folders is ' + folderName) for subfolder in subfolders: print('SUBFOLDER OF ' + folderName + ': ' + subfolder) for filename in filenames: print('FILE INSIDE ' + folderName + ': ' + filename) print('')
利用zipfile模块中的函数,Python程序可以创建和打开(解压)ZIP文件。假定,example.zip的文件,可以从http://nostarch.com/automatestuff/下载文件。
要读取ZIP文件的内容,首先必须创建一个ZipFile对象 。调用zipfile.ZipFile()函数,向它传入一个字符串,表示.zip文件的文件名,请注意,zipfile是Python模块的名称,ZipFile() 是函数的名称。
>>> import zipfile, os >>> os.chdir('C:\\PyProjects') >>> exampleZip = zipfile.ZipFile('example.zip') >>> exampleZip.namelist() ['spam.txt', 'cats/', 'cats/catnames.txt', 'cats/zophie.jpg'] >>> spamInfo = exampleZip.getinfo('spam.txt') >>> spamInfo.file_size 13908 >>> spamInfo.compress_size 3828 >>> 'Compressed file is %sx smaller!' % (round(spamInfo.file_size / spamInfo.compress_size, 2)) 'Compressed file is 3.63x smaller!' >>> exampleZip.close()
ZipFile对象的namelist()方法,返回ZIP文件中包含的所有文件和文件夹的字符串的列表。ZipInfo对象有自己的属性,诸如表示字节数的file_size和compress_size。分别表示原来文件大小和压缩后文件大小。ZipFile 对象表示整个归档文件,而ZipInfo 对象则保存该归档文件中每个文件的有用信息。
extractall() 方法从ZIP文件解压缩所有文件和文件夹,放到当前工作目录
>>> import zipfile, os >>> os.chdir('C:\\PyProjects') >>> exampleZip = zipfile.ZipFile('example.zip') >>> exampleZip.extractall() >>> exampleZip.close()
ZipFile对象的extract()方法从ZIP文件中解压缩单个文件
>>> exampleZip = zipfile.ZipFile('example.zip') >>> exampleZip.extract('spam.txt') 'C:\\PyProjects\\spam.txt' >>> exampleZip.extract('spam.txt', 'C:\\PyProjects\\cats') 'C:\\PyProjects\\cats\\spam.txt' >>> exampleZip.close()
传递给extract()的字符串,必须匹配namelist()返回的字符串列表中的一个。或者可以向extract() 传递第二个参数,将文件解压缩到指定的文件夹,而不是当前工作目录。如果第二个参数指定的文件夹不存在,Python就会创建它,extract() 的返回值是被压缩后文件的绝对路径。
zipfile.ZIP_DEFLATED,指定deflate压缩算法,对各种类型的数据很有效。
>>> import zipfile >>> newZip = zipfile.ZipFile('new.zip', 'w') >>> newZip.write('spam.txt', compress_type=zipfile.ZIP_DEFLATED) >>> newZip.close()
#!/usr/bin/env python # backupToZip.py - Copies an entire folder and its contents into a ZIP file whose filename increments. import zipfile, os def backupToZip(folder): # Backup the entire contents of "folder" into a ZIP file. folder = os.path.abspath(folder) # make sure folder is absolute # Figure out the filename this code shoulde use based on # what files already exist. numer = 1 while True: zipFilename = os.path.basename(folder) + '_' + str(numer) + '.zip' if not os.path.exists(zipFilename): break number = number + 1 # TODO: Create the ZIP file. print('Creating %s...' % (zipFilename)) backupZip = zipfile.ZipFile(zipFilename, 'w') # TODO: Walk the entire folder tree and compress the files in each folder. for foldername, subfolders, filenames in os.walk(folder): print('Adding files in %s...' % (foldername)) # Add the current folder to the ZIP file. backupZip.write(foldername) # Add all the files in this folder to the ZIP file. for filename in filenames: newBase / os.path.basename(folder) + '_' if filename.startswith(newBase) and filename.endswith('.zip'): continue # don't backup the backup ZIP file backupZip.write(os.path.join(foldername, filename)) print('Done.') backupToZip('C:\\PyProjects')