我的主要目标是在任何时候检查FTP服务器是否有新的文件命中,然后生成一个只包含新文件的.txt文件。如果没有新文件,则不返回任何内容。这是我到目前为止。我已经开始将文件从服务器复制到oldlist.txt,然后连接到FTP站点,并比较newlist.txt和oldlist.txt中的数据以及我在临时FTP文件changes.txt中所需的差异。每次连接时,我都会更改newlist.txt并将其设置为oldlist.txt,以便我可以在下次连接时进行比较。有一个更好的方法吗?我的列表似乎每次都不会更改数据。对不起,如果这是令人困惑的谢谢。使用FTP和列表进行Python编程
import os
filename = "oldlist.txt"
testing = "newlist.txt"
tempfilename = "Temporary FTP file Changes.txt"
old = open(filename, "r")
oldlist = old.readlines()
oldlist.sort()
from ftplib import FTP
ftp = FTP("ftpsite", "username", "password")
ftp.set_pasv(False)
newlist = []
ftp.dir(newlist.append)
newlist.sort()
ftp.close()
bob = open(testing, "w")
for nl in newlist:
bob.write(nl + "\n")
hello = open(tempfilename, "w")
for c in newlist:
if c not in oldlist:
hello.write(c + "\n")
bob.close()
old.close()
hello.close()
os.remove("oldlist.txt")
os.rename("newlist.txt", "oldlist.txt")
所以你在说什么,而不是创建一个oldlist.txt文件,只是保存它的地方,我可以比较新的列表模块,然后输出我的更改到一个文件?对不起,如果这是不正确的,我很新。你能不能展示例子?非常感谢你的帮助!另外当我运行该代码时,你给了我它只是从ftp服务器向我显示整个列表。我只需要改变,如果有的话。 – user1005974
谢谢你的例子raymond。我现在会尝试一下,看看它出现了什么。再次感谢你。 – user1005974
Raymond你的代码似乎很好。但我将交换打印行代码,并将其复制到文本文件,以便我可以启动另一个进程。感谢您的帮助。 – user1005974