我正在尝试在 Windows 8.1 和 python 2.7.9 上使用zipfile库。
我只想在 zipfile.open() 之后删除library.zip,但 os.remove() 会抛出“WindowsError [Error 32]”,并且 zipfile 似乎不会将 zip 文件从 with 块中释放出来。
WindowsError 32 的意思是“该进程无法访问该文件,因为该文件正在被另一个进程使用。”
那么,如何删除这个library.zip 文件呢?
代码:
import os
import zipfile as z
dirs = os.listdir('build/')
bSystemStr = dirs[0]
print("[-] Merging library.zip...")
with z.ZipFile('build/' + bSystemStr + '/library.zip', 'a') as z1:
with z.ZipFile('build_temp/' + bSystemStr + '/library.zip', 'r') as z2:
for t in ((n, z2.open(n)) for n in z2.namelist()):
try:
z1.writestr(t[0], t[1].read())
except:
pass
print("[-] Cleaning temporary files...")
os.remove('build_temp/' + …Run Code Online (Sandbox Code Playgroud) ZipFile我正在尝试从 URL读取数据并通过使用解析 as csvStringIO内的数据ZipFilepandas.read_csv
r = req.get("http://seanlahman.com/files/database/lahman-csv_2014-02-14.zip").content
file = ZipFile(StringIO(r))
salaries_csv = file.open("Salaries.csv")
salaries = pd.read_csv(salaries_csv)
Run Code Online (Sandbox Code Playgroud)
最后一行给了我一个错误:
CParserError: Error tokenizing data. C error: Calling read(nbytes) on source failed. Try engine='python'.
Run Code Online (Sandbox Code Playgroud)
但是如果我尝试使用
salaries = pd.read_csv(file.open("Salaries.csv"))
Run Code Online (Sandbox Code Playgroud)
有用。
所以我想知道我在这里错过了什么。
file.open应该返回一个ZipExtFile对象,并且由于 read_csv 仅接受字符串或文件句柄/StringIO输入,为什么最后一行可以工作?
我在 Ubuntu 中提取了一个 zip 文件(hisat2-2.2.0-Linux_x86_64.zip来自https://cloud.biohpc.swmed.edu/index.php/s/hisat2-220-Linux_x86_64/download)(通过右键单击该文件并选择在此处提取),并且这个给我以下文件权限:
total 91M
-rw-r--r-- 1 user user 584 Jul 9 2019 NEWS
-rw-r--r-- 1 user user 78K Jul 9 2019 MANUAL.markdown
-rw-r--r-- 1 user user 35K Jul 9 2019 LICENSE
-rwxr-xr-x 1 user user 2.8K Jul 9 2019 hisat2-build
-rw-r--r-- 1 user user 1.3K Jul 9 2019 AUTHORS
-rw-r--r-- 1 user user 202 Jan 23 2020 TUTORIAL
-rw-r--r-- 1 user user 64K Jan 23 2020 MANUAL
-rw-r--r-- 1 user user 6 Dec …Run Code Online (Sandbox Code Playgroud) 当我使用 ZIPCrypto 压缩时,这种方法起作用了。AES-256 失败了。请问如何解决这个问题?
我之前使用以下 Python 代码成功打开了使用 7-Zip 创建的受密码保护的 .zip 文件:
import zipfile
zip_file = zipfile.ZipFile('crack_me.zip')
output_verbose = 1 # increase that for long password list
with open('passwords.txt', 'rb') as password_list:
for index, line in enumerate(password_list):
try:
pwd = line.strip(b'\r\n')
zip_file.extractall(pwd=pwd)
except RuntimeError as e:
print(e)
if index % output_verbose == 0:
print('{}. The {} word not matched.'.format(index + 1, pwd))
else:
print('{}. Wow ! found the password: {}'.format(index + 1, pwd))
break
zip_file.close()
Run Code Online (Sandbox Code Playgroud)
然而,由于无法理解的原因,它在多次尝试中只成功了几次。大多数情况下,都会给出“不支持该压缩方法”的例外情况。
我尝试删除、重命名、重新创建 .zip 文件,但没有成功。它偶尔起作用对我来说毫无意义。
我尝试将问题简化如下:
import …Run Code Online (Sandbox Code Playgroud) 以下命令运行,但导致文件解压失败:
import io
import zipfile
b = io.BytesIO()
zf = zipfile.ZipFile(b, mode='w')
zf.writestr('a/b.txt', 'look here')
zf.writestr('a/c.txt', 'look there')
open('here.zip', 'wb').write(b.getbuffer())
Run Code Online (Sandbox Code Playgroud)
然后测试:
$ unzip here.zip
Archive: here.zip
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of here.zip or
here.zip.zip, and cannot find …Run Code Online (Sandbox Code Playgroud)