Python中文
首页
教程
问答
标签
搜索
登录
注册
Python If语句,在赋值之前引用错误局部变量“newfile”
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有下面的python2.7脚本。在</p> <p>该脚本遍历包含文件名的CSV文件,然后在FTP服务器上查找文件名。在</p> <p>但是,在ftp上找不到文件时,我得到一个错误:</p> <p>赋值前引用局部变量“newfile”时出错</p> <p>理想情况下,如果在ftp上找不到上一个文件,我希望脚本只移动到文件的下一行。我该怎么做?提前谢谢。在</p> <pre><code>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) </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>尝试改变这个:</p> <pre><code> if filedate: newfile = filename </code></pre> <p>在这方面:</p> ^{pr2}$
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在python中从数据帧列中删除分类值?
4 回答
如何在python中从数据帧列表中删除引号
10 回答
如何在python中从数据帧创建列表
10 回答
如何在Python中从数据帧创建嵌套的JSON
8 回答
如何在Python中从数据帧显示wordcloud
6 回答
如何在Python中从数据帧的时间戳中删除字符
5 回答
如何在Python中从数据帧绘制简单绘图?
3 回答
如何在python中从数据帧行提取具有特定长度的范围?
9 回答
如何在python中从数据帧设置dict中的值
9 回答
如何在Python中从数据库中获得一个结果
7 回答
如何在python中从数据框中绘制分类条形图
7 回答
如何在Python中从数据框中选择特定细节?
8 回答
如何在python中从数据集中删除unicode
9 回答
如何在python中从数据集中删除某些数值?
7 回答
如何在python中从数据集中选择行
2 回答
如何在Python中从数组中删除元素
8 回答
如何在python中从数组中删除单个倒逗号?
10 回答
如何在python中从数组中删除对象?
3 回答
如何在python中从数组中删除引号
2 回答
如何在python中从数组中删除所有最小值
5 回答