调用后无法访问使用python zipfile库创建的Zip文件zipfile.关闭()

2024-09-29 00:21:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个python脚本,创建一个zip文件,然后将其上传(FTP)到服务器。但是,一旦我创建了zip文件(并关闭它),我就会得到一个

[Errno 2]没有这样的文件或目录:“2014.3.20.17.3.42。测试员.zip'

但是,我可以在z.close之后设置断点,并浏览(不是通过python代码)到该位置,并且该文件存在并且可以访问。你知道吗

据我所知,我唯一要做的就是关闭zip文件。在z.close()之后,我还需要做些什么,以便在代码中可以访问它吗?你知道吗

如果有关系的话,我现在正在使用Python2.6。你知道吗

代码:

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()

Tags: 文件代码fromimportclosedatetimeosftp