发布网友 发布时间:2022-04-23 01:22
共3个回答
热心网友 时间:2022-04-18 04:39
#!/usr/bin/env python3
import os,zipfile
def Zip(target_dir):
target_file=os.path.basename(os.getcwd())+'.zip'
zip_opt=input("Will you zip all the files in this dir?(Choose 'n' you should add files by hand)y/n: ")
while True:
if zip_opt=='y': #compress all the files in this dir
filenames=os.listdir(os.getcwd()) #get the file-list of this dir
zipfiles=zipfile.ZipFile(os.path.join(target_dir,target_file),'w',compression=zipfile.ZIP_DEFLATED)
for files in filenames:
zipfiles.write(files)
zipfiles.close()
print("Zip finished!")
break
elif zip_opt=='n': #compress part of files of this dir
filenames=list(input("Please input the files' name you wanna zip:"))
zipfiles=zipfile.ZipFile(os.path.join(target_dir,target_file),'w',compression=zipfile.ZIP_DEFLATED)
for files in filenames:
zipfiles.write(files)
zipfiles.close()
print("Zip finished!")
break
else:
print("Please in put the character 'y' or 'n'")
zip_opt=input("Will you zip all the files in this dir?(Choose 'n' you should add files by hand)y/n: ")
def Unzip(target_dir):
target_name=input("Please input the file you wanna unzip:")
zipfiles=zipfile.ZipFile(target_name,'r')
zipfiles.extractall(os.path.join(target_dir,os.path.splitext(target_name)[0]))
zipfiles.close()
print("Unzip finished!")
def main():
opt=input("What are you gonna do?Zip choose 'y',unzip choose 'n'.y/n: ")
while True:
if opt=='y': #compress files
zip_dir=input("Please input the absdir you wanna put the zip file in:")
Zip(zip_dir)
break
elif opt=='n': #unzip files
unzip_dir=input("Please input the absdir you wanna put the zip file in(Nothing should be done if you wann unzip files in the current dir):")
if unzip_dir=='':
Unzip(os.getcwd())
else:
Unzip(unzip_dir)
break
else:
print("Please input the character 'y' or 'n'")
opt=input("What are you gonna do?Zip choose 'y',unzip choose 'n'.y/n: ")
if __name__=='__main__':
main()
解压和压缩都有,自己稍微改改解压的文件名就行了追问其实我是将文件用urllib.request.urlretrieve下载到本地 存放到d:/test/2.zip
我现在需要吧他解压到d:/test/2文件夹里面 不需要交互的
代码越简单越好 也不需要压缩功能的 能否吧代码精简下 万分感谢
热心网友 时间:2022-04-18 05:57
python没有rar模块的
热心网友 时间:2022-04-18 07:31
用2.X的飘过~追问内置函数库 应该都差不多的