标签:功能设计 inpu The module erro -- base compress mini
案例: 代码备份机# 第一步:根据功能设计函数
# 1.打包
def zip_all():
‘‘‘打包‘‘‘
pass
def auto_name():
‘‘‘自动命名‘‘‘
pass
def zip_all_by_name():
‘‘‘筛选文件‘‘‘
pass
# 第二步:完善函数输入参数和返回值,并逐一测试
# 1.打包
import zipfile
import os
def zip_all(from_dir, target_name):
‘‘‘传入目标目录,打包的名称这两个参数‘‘‘
my_zip = zipfile.ZipFile(target_name,‘w‘)
for root, dirs, files in os.walk(from_dir):
for name in files:
filename = os.path.join(root, name)
my_zip.write(filename, compress_type=zipfile.ZIP_DEFLATED)
my_zip.close()
def auto_name(source_name):
new_name = ‘1.zip‘
# 判断文件是否存在,单纯if只是判断是否为空
# 还要进一步判断
if source_name: # a-1.zip a-2.zip
# split拆分
new_name = source_name.split(‘-‘)[0] + ‘-‘ + str(int(source_name.split(‘-‘)[1].split(‘.‘)[0]) + 1) + ‘.zip‘
return new_name
base_dir = r‘C:\study‘
target_name = os.path.join(base_dir, auto_name(‘c-1.zip‘))
from_dir = os.path.join(base_dir, ‘jupyter‘)
zip_all(from_dir, target_name)
coop = zipfile.ZipFile(r‘C:\study\c-2.zip‘)
coop.namelist
# os.chdir(r‘C:\study‘)
# coop.getinfo(‘c-2.zip‘)
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-4-67f8c6decc4e> in <module>()
29 coop.namelist
30 os.chdir(r‘C:\study‘)
---> 31 coop.getinfo(‘c-2.zip‘)
c:\users\coop\miniconda3\envs\coop\lib\zipfile.py in getinfo(self, name)
1279 if info is None:
1280 raise KeyError(
-> 1281 ‘There is no item named %r in the archive‘ % name)
1282
1283 return info
KeyError: "There is no item named ‘c-2.zip‘ in the archive"
os.getcwd()
‘C:\\study‘
标签:功能设计 inpu The module erro -- base compress mini
原文地址:http://blog.51cto.com/13118411/2138666