我正在 python 中使用 FTPLib 上传文件,并且有一个带有 progressbar 2.2. 的 cli 加载栏 我需要制作一个加载栏来告诉上传进度。
有人有关于该主题的任何信息吗?
谢谢,乔达梅里奥
正如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()
最佳答案
对代码进行小更改:
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
。
关于Python FTP上传栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5673170/