[英]Zip file created with python zipfile library is not accessible after calling zipfile.close()
I am trying to write a python script that creates a zip file, and then uploads (FTP's) it to a server. 我正在尝试编写一个Python脚本,该脚本创建一个zip文件,然后将其上载(FTP的)到服务器。 However once I create the zip file (and close it) I am getting a 但是,一旦我创建了zip文件(并将其关闭),我就会得到一个
[Errno 2] No such file or directory: '2014.3.20.17.3.42.tester.zip' [Errno 2]没有这样的文件或目录:'2014.3.20.17.3.42.tester.zip'
However I can set breakpoint just after z.close, and browse (not via python code) to the location, and the file exists, and is accessible. 但是我可以在z.close之后设置断点,然后浏览(而不是通过python代码)到该位置,并且该文件存在并且可以访问。
From what I can tell the only thing I should have to do is close the zip file. 据我所知,我唯一要做的就是关闭zip文件。 Is there something else I need to do to after z.close() so that it will be accessible in the code? 在z.close()之后,我还需要做其他事情以便在代码中访问它吗?
If it matters I am currently using Python 2.6. 如果有关系,我目前正在使用Python 2.6。
Code: 码:
import datetime
import os
from zipfile import ZipFile
from zipfile import ZipInfo
import zipfile
Directory = r"C:\Users\myUserAcct\Documents\output"
theDateTime = datetime.datetime.now()
theDateTimeFormatted = str(theDateTime.year) + '.' + str(theDateTime.month) + '.' + str(theDateTime.day) + '.' + str(theDateTime.hour) + '.' + str(theDateTime.minute) + '.' + str(theDateTime.second)
theZipFileName = theDateTimeFormatted + '.tester.zip'
z = ZipFile(theZipFileName,'w', zipfile.ZIP_DEFLATED)
os.chdir(Directory)
filelist = os.listdir(".")
for f in filelist:
z.write(f)
z.close()
tehFile = open(theZipFileName,'rb')
# Then i will upload (FTP) this file
tehFile.close()
You are doing a os.chdir(Directory)
after making the ZipFile
instance. 制作ZipFile
实例后,您正在执行os.chdir(Directory)
。 Change the directory before you make the instance and it should work. 在创建实例之前更改目录,它应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.