Python - 如果语句,在赋值之前引用错误局部变量'newfile'

时间:2018-02-15 09:17:37

标签: python python-2.7 pysftp

我遵循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)

3 个答案:

答案 0 :(得分:0)

你的问题在于这段代码:

for filename in possibleFiles:
        filedate = re.search(string, filename)  
        if filedate:
            newfile = filename

newfile仅定义if filedate。之一:

  • 检查您的输入,以便if filedate返回True
  • 将所有后续代码放在if子句下(通过额外缩进),以便只执行if filedate == True

答案 1 :(得分:0)

尝试更改此内容:

            if filedate:
                newfile = filename

进入这个:

            if filedate and filename is not none:
                newfile = filename

答案 2 :(得分:0)

问题出在这一行:

timestamp  = oftp.stat(newfile).st_atime

如果之前未根据if条件分配newfile,即使未分配,也会引用它。

在脚本的开头,如@cco所建议,您还可以将newfile设置为空字符串: newfile = ''

您应该进行结构化,以便仅在分配newfile时处理其余部分。您可以在newfile上添加条件。

另外,如果我没有弄错,以下行应该是for循环的一部分。否则newfile将只包含找到的最后一个文件名。每次找到下一个时都会被覆盖。

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"
相关问题