简体   繁体   中英

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. However once I create the zip file (and close it) I am getting a

[Errno 2] No such file or directory: '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.

From what I can tell the only thing I should have to do is close the zip file. Is there something else I need to do to after z.close() so that it will be accessible in the code?

If it matters I am currently using 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. Change the directory before you make the instance and it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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