我有以下 Python 2.7 脚本。
该脚本遍历包含文件名的 CSV 文件,然后在 FTP 服务器上查找文件名。
但是,当在 ftp 上找不到该文件时出现错误:
赋值前引用的错误局部变量“newfile”
理想情况下,如果在 ftp 上找不到前一个文件,我希望脚本只移动到文件中的下一行。我该怎么做?提前致谢。
def googleclouda(googlefile):
import time
import pysftp
import sys
import os
from os import path
from datetime import datetime
import calendar
import zipfile
import re
os.chdir("C:\Users\\xxx\python\\xxx\\")
oftp = pysftp.Connection(host="xxxxxx", username="xxxxxx", password="xxxxxx")
d = datetime.utcnow()
unixtime=calendar.timegm(d.utctimetuple())
month = datetime.now().strftime("%m")
string = googlefile+month+".*\.txt$"
possibleFiles = oftp.listdir("/")
for filename in possibleFiles:
filedate = re.search(string, filename)
if filedate:
newfile = filename
timestamp = oftp.stat(newfile).st_atime
if timestamp > unixtime - 604800:
newtime=unixtime + 3600
gaamfile='file_123_26807_'
zipname = gaamfile+str(newtime)+'.sync.zip'
create_zip = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED)
oftp.get(newfile, newfile)
oftp.close()
newfilename = gaamfile+str(newtime)+'.sync'
os.rename(newfile, newfilename)
create_zip.write(newfilename)
create_zip.close()
print newfile
else:
print "No files found"
filecsv = 'filelist.csv'
with open(filecsv, 'r') as f:
for line in f:
if not line.startswith('\n'):
googlefile = line.strip()
googleclouda(googlefile)