我正在尝试打开存档中的特定文件,然后向其中写入一些内容。我正在使用zipfile.open()函数来访问文件:
import zipfile
my_zip = zipfile.ZipFile('D:\\files\\acrhive.zip')
with my_zip.open('hello.txt', 'w') as my_file:
my_file.write(b'Hello')
my_zip.close()
但是,它给了我一个关于名为“hello.txt”的重复文件的警告。之后,我收到以下错误:
ValueError: write() requires mode 'w', 'x', or 'a'
我在这里做错了什么?
我的完整追溯:
D:\python\lib\zipfile.py:1506: UserWarning: Duplicate name: 'hello.txt'
return self._open_to_write(zinfo, force_zip64=force_zip64)
Traceback (most recent call last):
File "D:\files\python.py", line 8, in <module>
with my_zip.open(file, 'w') as my_file:
File "D:\python\lib\zipfile.py", line 1506, in open
return self._open_to_write(zinfo, force_zip64=force_zip64)
File "D:\python\lib\zipfile.py", line 1598, in _open_to_write
self._writecheck(zinfo)
File "D:\python\lib\zipfile.py", line 1699, in _writecheck
raise ValueError("write() requires mode 'w', 'x', or 'a'")
ValueError: write() requires mode 'w', 'x', or 'a'