目前用户输入被保存为计算机上的文件,我想也希望它也可以上传到我的ftp服务器,我有点工作,但文件中没有任何内容,很可能因为我是编程新手,所以很容易出错,谢谢
quotation = input("Do you want to save a quotation? ")
if (quotation == "yes") :
quotationName = input("What do you want to save the quotation as? ")
text_file = open("{}.txt".format(quotationName),"w") #(w) opens files in write
text_file.write ("The total lawn area is {}\n".format(totalAreaLawn))
text_file.write ("The total for the lawn is £{}\n\n".format(totalCostLawn))
text_file.write ("The total concrete area is {}\n".format(totalAreaConcrete))
text_file.write ("The total for the concrete is £{}\n\n".format(totalCostConcrete))
text_file.write ("The total wooden deck area is {}\n".format(totalAreaWoodenDeck))
text_file.write ("The total for the wooden deck is £{}\n\n".format(totalCostWoodenDeck))
text_file.write ("The total rectangular pond area is {}\n".format(totalAreaRectangularPond))
text_file.write ("The total for the wooden deck is £{}\n\n".format(totalCostRectangularPond))
text_file.write ("The total number of water features needed is {}\n".format(numberOfWaterFeatures))
text_file.write ("The total for the water features is £{}\n\n".format(totalCostWaterFeatures))
text_file.write ("The total number of garden lights needed is {}\n".format(numberOfGardenLights))
text_file.write ("The total for the garden lights is £{}\n\n".format(totalCostGardenLighting))
#text_file.close()
ftpconnect = ftplib.FTP('landscapegardening.freeiz.com','a6011438','L0g1tecH')
saveDirectory = '/public_html/Quote'
ftpconnect.cwd(saveDirectory)
fileSend = open("{}.txt".format(quotationName),'rb')
ftpconnect.storbinary('STOR {}.txt'.format(quotationName), fileSend)
fileSend.close()
ftpconnect.quit()
print("Quotation saved")
答案 0 :(得分:0)
为什么你评论了text_file.close()
行?使用这样的代码,无法保证在您要发送文件时将任何内容写入文件。我认为最好的方法是先关闭文件(只需再次在close()行中注释),或者如果你不想重新打开同一个文件来调用text_file.flush()后跟os.fsync(text_file)。如果您选择第二个选项,请记住将ftpconnect.storbinary('STOR {}.txt'.format(quotationName), fileSend)
更改为ftpconnect.storbinary('STOR {}.txt'.format(quotationName), text_file)
。您还必须将打开模式更改为“r +”并使用text_file.seek(0)
转到文件的开头。
编辑:刚看到你实际上只需要一个类似文件的对象,详见这里: Can I upload an object in memory to FTP using Python?