Python3-笔记-F-001-组织文件

import shutil, os, zipfile

# 复制、移动、改名、删除文件与文件夹
#shutil.copy('e:\\111\\key.txt', 'd:\\111\\') # copy文件
#shutil.copytree('e:\\111', 'd:\\111\\aaa') # copy目录。且目标文件夹需不存在
#shutil.move('e:\\111\\key.txt', 'e:\\111\\key2.txt') # 改文件名
#shutil.move('e:\\111', 'e:\\112') # 改文件夹名
#shutil.move('e:\\112\\key2.txt', 'd:\\111\\key22.txt') # 移动文件
#shutil.move('e:\\112', 'd:\\111') # 移动文件夹
#shutil.rmtree('d:\\111\\112') # 不可恢复地删除文件夹
#os.unlink('d:\\111\\key22.txt') # 删除文件

'''#删除文件或文件夹到回收站
import send2trash
send2trash.send2trash('d:\\111')
baconFile = open('bacon.txt', 'a') # creates the file
baconFile.write('Bacon is not a vegetable.')
baconFile.close()
send2trash.send2trash('bacon.txt')


# 遍历目录树,压缩到zip
newZip = zipfile.ZipFile('c:\\new.zip', 'w')
for folderName, subfolders, filenames in os.walk('D:\\xiaomi'):
print('The current folder is ' + folderName)
for subfolder in subfolders:
print('SUBFOLDER OF ' + folderName + ': ' + subfolder)
newZip.write(folderName+'\\'+subfolder, compress_type=zipfile.ZIP_DEFLATED)
for filename in filenames:
print('FILE INSIDE ' + folderName + ': '+ filename)
newZip.write(folderName+'\\'+filename, compress_type=zipfile.ZIP_DEFLATED)
print('')
newZip.close()

'''
# 解压缩zip到指定文件夹
exampleZip = zipfile.ZipFile('c:\\new.zip')
for file_name in exampleZip.namelist():
print('File:', file_name, end = ' ')
file_bytes = exampleZip.read(file_name)
print('has ', len(file_bytes), ' bytes', end = ' ')
spamInfo = exampleZip.getinfo(file_name)
if spamInfo.file_size and spamInfo.compress_size:
print('Compressed file is %sx smaller!' % (round(spamInfo.file_size / spamInfo.compress_size, 2)))
print()
# exampleZip.extractall("d:\\333")
# exampleZip.extract('xiaomi/MiPhoneManager/LocalCache/6O5513A60360/' , 'd:\\333\\') # 可以解压一个文件或一个空文件夹名称
exampleZip.close()

print('')
posted @ 2017-10-26 15:42  魏桐  阅读(270)  评论(0编辑  收藏  举报