我想在文件开始上传到FTP服务器时以及每当ftp目录中没有文件上传超过10分钟时收到通知。有没有方法可以告诉我文件是否已经开始在FTP服务器上传(在Python中)?
我认为以下代码将解决您只需将其与服务器连接的问题。您也可以在本地目录中尝试。
import os, time
path_to_watch = "test_ftp/"
flag = 0
before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while 1:
time.sleep (10)
after = dict ([(f, None) for f in os.listdir (path_to_watch)])
added = [f for f in after if not f in before]
print added
removed = [f for f in before if not f in after]
if added:
print "Added: ", ", ".join (added)
if flag==0:
print "Notify me once"
flag =1
if removed: print "Removed: ", ", ".join (removed)
if after == before:
print "No files uploaded in 10 minutes"
break
before = after