我写这个程序是为了从网站目录上的一个文本文件中提取数据(该目录由网站上的用户编辑),但它似乎崩溃了。很多。
from sys import argv
import ftplib
import serial
from time import sleep
one = "0"
repeat = True
ser = serial.Serial("COM3", 9600)
while repeat == True:
path = 'public_html/'
filename = 'fileone.txt'
ftp = ftplib.FTP("*omitted*")
ftp.login("*omitted*", "*omitted*")
ftp.cwd(path)
ftp.retrbinary("RETR " + filename ,open(filename, 'wb').write)
ftp.quit()
txt = open(filename)
openup = txt.read()
ser.write(openup)
print(openup)有没有人知道什么方法可以阻止它崩溃?我在考虑使用异常,但我不是Python专家。顺便说一句,程序做了它想做的事情,由于显而易见的原因,地址和登录信息被省略了。另外,如果可能的话,我会请求一个异常来阻止程序在与串行端口断开连接时崩溃。
提前感谢!
发布于 2016-04-15 01:46:39
两件事:
try:
#code related to ftplib
except Exception, e: #you can fill this in after you encounter the exception once
print str(e)
with open(filename, 'r') as txt:
openup = txt.read()这样,一旦你在“with”块之外,文件就会自动关闭。
https://stackoverflow.com/questions/36629964
复制相似问题