Python FTP 上传进度条
我正在用Python的FTPLib上传一个文件,并且有一个命令行的加载条,使用的是progressbar 2.2。我需要做一个加载条来显示上传的进度。
有没有人对此有一些信息可以分享?
谢谢,giodamelio
正如Senthil Kumaran提到的,ftplib.storbinary函数里有一个回调参数,但我不知道怎么用。
我试过这个。我希望每次上传一个字节时,它都能打印一条消息。
import ftplib
def callback():
print("This is the callback function")
s = ftplib.FTP('myserver.com','login','password') # Connect
f = open('test.txt','rb') # file to send
s.storbinary('STOR test.txt', f, 1024, callback()) # Send the file
f.close() # Close file and FTP
s.quit()
2 个回答
2
如果你能具体问一下,比如给出你尝试过的代码示例,那会更有助于得到答案。使用指示器显示进度是可以的,前提是FTP库提供了某些功能,比如callback
函数。你需要用你的进度指示器函数(在这个例子中是进度条)并把它连接到那个回调上。查看一下ftplib的文档,里面有一些方法可以连接回调,也许这对你会有帮助。
4
你只需要对你的代码做个小改动:
import ftplib
def callback(p):
print("This is the callback function")
s = ftplib.FTP('myserver.com','login','password') # Connect
f = open('test.txt','rb') # file to send
s.storbinary('STOR test.txt', f, 1024, callback) # Send the file
f.close() # Close file and FTP
s.quit()
这个回调函数需要稍后再调用。如果你在把它作为参数传递的时候就直接调用它,那么它的返回值会被传递出去。因为你的 callback
函数没有返回任何东西,所以它会传递 None
。