练习使用zipfile中的函数,.py和zip在同一工作目录下,os.chdir也已转到该目录下。
import shutil, zipfile,os
print(os.getcwd())
os.chdir('D:\\chwlsw\\py-test')
exampleZip = zipfile.ZipFile('test_1124.zip')
print(exampleZip.namelist())
print(type(exampleZip))
docInfo = exampleZip.getinfo('namelist.txt')
###### 运行结果及报错内容
Traceback (most recent call last):
['test_1124/', 'test_1124/namefile.txt', 'test_1124/namefile_1.txt', 'test_1124/namefile_2.txt', 'test_1124/namefile_3.txt', 'test_1124/os_read_write/', 'test_1124/os_read_write/bacon.txt', 'test_1124/os_read_write/hello.txt', 'test_1124/os_read_write/mycats.py', 'test_1124/os_read_write/mydata.bak', 'test_1124/os_read_write/mydata.dat', 'test_1124/os_read_write/mydata.dir', 'test_1124/os_read_write/osPathMethods.txt', 'test_1124/os_read_write/os_files_testing.py', 'test_1124/os_read_write/poem.txt', 'test_1124/os_read_write/__pycache__/', 'test_1124/os_read_write/__pycache__/mycats.cpython-39.pyc']
<class 'zipfile.ZipFile'>
File "D:\chwlsw\py-test\test_sys.py", line 11, in <module>
docInfo = exampleZip.getinfo('namelist.txt')
File "D:\chwlsw\Python39\lib\zipfile.py", line 1429, in getinfo
raise KeyError(
KeyError: "There is no item named 'namelist.txt' in the archive"
你放的图叫namefile.txt啊,哪里有namelist.txt
你确定想传入的是这个,而不是exampleZip.namelist()吗
博主问题解决了吗