简体   繁体   English

python: zipfile.ZipFile 没有那个文件或目录

[英]python: zipfile.ZipFile No such file or directory

There is folder path:有文件夹路径:

P:\\2018\\Archive\\

There are many zipfiles I want to create programmatically, but am starting with test.我想以编程方式创建许多 zip 文件,但我从测试开始。 I will name this test zip file "CO_007_II.zip" and will attempt to create in above location:我将此测试 zip 文件命名为“CO_007_II.zip”,并将尝试在上述位置创建:

import zipfile as zp

with zp.ZipFile("P:\\2018\\Archive\\CO_007_II.zip",'w') as myzip:
    myzip.write(r"P:\2018\CO_007_II")

But I get error!但我得到错误!

... ...

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python27\ArcGIS10.2\lib\zipfile.py", line 752, in __init__
    self.fp = open(file, modeDict[mode])
IOError: [Errno 2] No such file or directory: 'P:\\2018\\Archive\\CO_007_II.zip'

Is this not method for creating new zipfile?这不是创建新压缩文件的方法吗? I know file does not exist.我知道文件不存在。 Is why I am using 'w' mode, no?这就是我使用“w”模式的原因,不是吗?

This is documentation:这是文档:

https://docs.python.org/3/library/zipfile.html https://docs.python.org/3/library/zipfile.html

It says:它说:

'w' to truncate and write a new file 'w' 截断并写入新文件

Example on documentation page:文档页面上的示例:

with ZipFile('spam.zip', 'w') as myzip:
    myzip.write('eggs.txt')

code worked two days ago to create new zip file but did not add folder.两天前的代码可以创建新的 zip 文件,但没有添加文件夹。 Today nothing works?今天什么都没用? Why not.为什么不。 All paths valid?所有路径都有效吗? How do I create new zip file with python and add folders to it?如何使用 python 创建新的 zip 文件并向其中添加文件夹?

I had the same issue.我遇到过同样的问题。 It was the long path .这是一条漫长的道路 I solved by adding this //?/C at the beginning of the path我通过在路径的开头添加这个//?/C来解决

path = r"//?/C:\Users\Camilo\Proyectos"

The only way this could be reproduced was to create a zipfile in a directory that does NOT exist yet.可以复制的唯一方法是在尚不存在的目录中创建一个 zipfile。 The only way to be sure (you cannot trust a file manager; only way to verify is to check from within the program itself) is to assign the desired path of the new zip file to a variable (eg path), and then call isdir(dirname(path)) .唯一确定的方法(您不能信任文件管理器;唯一的验证方法是从程序本身内部检查)是将新 zip 文件的所需路径分配给变量(例如路径),然后调用isdir(dirname(path)) For example:例如:

from os.path import isdir
from os.path import dirname

target = "P:\\2018\\Archive\\CO_007_II.zip"
if not isdir(dirname(target)):
    print('cannot create zipfile because target does not exists')
else:
    # create the zipfile

I also encountered a similar issue and came here looking for answers.我也遇到了类似的问题,来到这里寻找答案。 Since this was the top hit, I'll add what I discovered.由于这是最受欢迎的,我将添加我发现的内容。

The answer provided by @metatoaster didn't work for me, when stepping through the code I found that the path returned true to isdir. @metatoaster 提供的答案对我不起作用,在单步执行代码时,我发现路径对 isdir 返回 true。

In my case, the path length exceeded the Windows max path length (260 chars) which was causing it to fail despite the folder path being valid.就我而言,路径长度超过了 Windows 最大路径长度(260 个字符),这导致它失败,尽管文件夹路径有效。

Hope that helps someone else down the line!希望能帮助其他人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM