天天看點

python檔案、檔案夾、壓縮包處理子產品-shutil子產品

shutil子產品 進階的檔案、檔案夾、壓縮包 處理子產品

shutil.copyfile(src, dst)

shutil.copyfile("shell.py","/usr/local/src/shell.py")

>> import shutil >> os.system("ls -lh /srv") 總用量 0 >> shutil.make_archive("/srv/www","gztar",root_dir="/root/11") '/srv/www.tar.gz' 總用量 4.0K -rw-r--r-- 1 root root 183 11月 22 15:59 www.tar.gz

import zipfile

#壓縮

>> import zipfile >> z = zipfile.ZipFile("shell.zip","w") >> z.write("shell.py") >> z.close() >> os.system("ls -lh ") #解壓 >> z = zipfile.ZipFile("shell.zip","r") >> z.extractall() #可設定解壓位址
>> import tarfile >> tar = tarfile.open("tar.tar","w") >> tar.add("/root/shell.zip",arcname="shell.zip") >> tar.add("/root/www.tar.gz",arcname="www.tar.gz") >> tar.close() >> tar = tarfile.open("tar.tar","r") >> tar.extractall() #可設定解壓位址

     本文轉自506554897 51CTO部落格,原文連結:http://blog.51cto.com/506554897/2045806,如需轉載請自行聯系原作者