【发布时间】:2011-12-13 16:47:29
【问题描述】:
刚刚写了我的第一个python程序!我将 zip 文件作为邮件附件保存在本地文件夹中。该程序检查是否有任何新文件,如果有,它会提取 zip 文件并根据文件名提取到不同的文件夹。当我运行我的代码时,我收到以下错误:
Traceback(最近一次调用最后一次):文件“C:/Zip/zipauto.py”,第 28 行,在 new_files 中的文件中:TypeError:'NoneType' 对象不可迭代
谁能告诉我哪里出错了。
非常感谢您的宝贵时间,
纳文 这是我的代码:
import zipfile
import os
ROOT_DIR = 'C://Zip//Zipped//'
destinationPath1 = "C://Zip//Extracted1//"
destinationPath2 = "C://Zip//Extracted2//"
def check_for_new_files(path=ROOT_DIR):
new_files=[]
for file in os.listdir(path):
print "New file found ... ", file
def process_file(file):
sourceZip = zipfile.ZipFile(file, 'r')
for filename in sourceZip.namelist():
if filename.startswith("xx") and filename.endswith(".csv"):
sourceZip.extract(filename, destinationPath1)
elif filename.startswith("yy") and filename.endswith(".csv"):
sourceZip.extract(filename, destinationPath2)
sourceZip.close()
if __name__=="__main__":
while True:
new_files=check_for_new_files(ROOT_DIR)
for file in new_files: # fails here
print "Unzipping files ... ", file
process_file(ROOT_DIR+"/"+file)
【问题讨论】:
标签: python