Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ftp不传输完整的文件_Python_Ftp - Fatal编程技术网

Python ftp不传输完整的文件

Python ftp不传输完整的文件,python,ftp,Python,Ftp,我有以下代码将文件传输到另一台linux机器: import ftplib session = ftplib.FTP('192.168.1.111','ubuntu','ubuntu') file = open('/home/nehal/darknet/yolo.weights','rb') # file to send print(session.pwd()) print(ftplib.FTP.dir(session)) session.storbinary(

我有以下代码将文件传输到另一台linux机器:

import ftplib
session = ftplib.FTP('192.168.1.111','ubuntu','ubuntu')
file = open('/home/nehal/darknet/yolo.weights','rb')                  # file to send
print(session.pwd())
print(ftplib.FTP.dir(session))
session.storbinary('STOR /home/ubuntu/yolo.weights',file)                  #send the file
file.close()
session.quit()

文件
yolo.weights
的大小为209MB,只传输了很少的MB

我还尝试传输一个30MB的文件,但只传输了几MB,此后似乎没有传输任何数据。

可能有什么问题?

使用
STOR
时,您应该只传递文件名,而不传递路径。因此,为了确保文件在正确的位置结束,请使用
.cwd()
首先指定目标目录:

import ftplib

session = ftplib.FTP('192.168.1.111','ubuntu','ubuntu')
file = open('/home/nehal/darknet/yolo.weights','rb')                  # file to send
print(session.pwd())
print(ftplib.FTP.dir(session))

session.cwd('/home/ubunto')
session.storbinary('STOR yolo.weights',file)                  #send the file

file.close()
session.quit()    
或者,您可以尝试以下操作:

import ftplib

session = ftplib.FTP('192.168.1.111', 'ubuntu', 'ubuntu')
file = open('/home/nehal/darknet/yolo.weights', 'rb')

with session, file:
    print(session.pwd())
    print(ftplib.FTP.dir(session))
    session.cwd('/home/ubunto')
    session.storbinary('STOR yolo.weights', file)

尝试从
STOR
中删除路径。你可以试试
session.cwd('/home/ubuntu')
然后就
STOR yolo.weights
@MartinEvans它成功了!非常感谢。请将此作为答案发布。原因可能是什么?当指定了整个路径时,为什么ftp不工作?AFAIK它是协议规范的一部分。你只是给它当前目录的文件名。好的!谢谢大家!@MartinEvans如果你是对的,我会理解如果服务器立即拒绝
STOR
命令,我不明白为什么传输会发生,但会在几MB后中止。