无法使用python 2.6通过FTP发送文件

无法使用python 2.6通过FTP发送文件,python,ftp,ftp-client,Python,Ftp,Ftp Client,我尝试了下面的代码,并更改了所有可能的方式,如 storbinary到storlines和r到rb和rb+,但即使无法将文件传输到服务器。以下是我的示例代码: from ftplib import FTP ftpfile = FTP('hostname') print "Connected with server" ftpfile.cwd('path of server where file need to store') print "Reached t

我尝试了下面的代码,并更改了所有可能的方式,如

storbinarystorlinesrrbrb+,但即使无法将文件传输到服务器。以下是我的示例代码:

    from ftplib import FTP
    ftpfile = FTP('hostname')
    print "Connected with server"
    ftpfile.cwd('path of server where file need to store')
    print "Reached to target directory"
    myFile = open(inputfile, 'rb+')
    ftpfile.storbinary('STOR ' +inputfile, myFile)
    print "transferring file..."
    myFile.close()
    print "file closed"
    ftpfile.quit()
    print "File transferred"
<代码>简单地运行并输出所有打印语句,但是当我签入服务器时,没有创建文件。请考虑登录成功。< /P>
需要建议以达到预期的产出。谢谢

您没有登录,因此无法执行任何操作。您确定已设置
inputfile

from ftplib import FTP
ftp = FTP('hn')
ftp.login('username', 'password')
ftp.cwd('working_dir')
myfile = open(myfile.txt, 'rb')
ftp.storbinary('STOR ' + myfile, myfile)
ftp.quit()
# No need to close() after quit.
或者,您可以在打开连接时使用以下方法登录:

ftp = FTP('hn', 'username', 'password')
因此,更好的是:

from ftplib import FTP
ftp = FTP('hn', 'username', 'pass')
ftp.cwd('working_dir')
with open(myfile, 'rb') as f:
    ftp.storbinary('STOR ' + myfile, f)

ftp.quit()
你需要传递,因为你正在传递一条路径

您还需要使用
storlines
,因为您发送的文件只是一个纯文本文件

试试这个:

import os
from ftplib import FTP

local_file = r'C:\working\cyborg.txt'
remote_file_name = os.path.basename(local_file)

ftp = FTP('host', 'username', 'password')
ftp.cwd('/some/path/on/server')
ftp.storlines('STOR %s' % (remote_file_name,),
              open(local_file, 'r'))
ftp.quit()

您应该检查
storebinary
的响应,什么是
inputfile
?@BurhanKhalid输入文件在我的本地系统中是文本文件。e、 g inputfile='c:\working\cyborg.txt'如果不检查任何命令的结果,您怎么可能知道发生了什么。这是一个翅膀上的代码和一个祈祷文。@RolfofSaxony这是一个很好的例子,不管怎么说,问题解决了,谢谢大家记录成功,我已经更新了我的问题。请将你的代码与问题中给出的代码进行比较,基本上两者都是相同的,它还不起作用。是的,但排除这一点,尝试是有价值的。你检查文件权限了吗?我试过了,伙计,不管怎样都解决了问题,非常感谢你。:)请以字符串格式添加0。e、 g ftp.storlines('STOR{0}'。格式(远程文件名)