我试图将文件从Windows服务器上传到unix服务器(基本上尝试做FTP).我使用下面的代码
#!/usr/bin/python
import ftplib
import os
filename = "MyFile.py"
ftp = ftplib.FTP("xx.xx.xx.xx")
ftp.login("UID", "PSW")
ftp.cwd("/Unix/Folder/where/I/want/to/put/file")
os.chdir(r"\\windows\folder\which\has\file")
ftp.storbinary('RETR %s' % filename, open(filename, 'w').write)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Traceback (most recent call last):
File "Windows\folder\which\has\file\MyFile.py", line 11, in <module>
ftp.storbinary('RETR %s' % filename, open(filename, 'w').write)
File "windows\folder\Python\lib\ftplib.py", line 466, in storbinary
buf = fp.read(blocksize)
AttributeError: 'builtin_function_or_method' object has no attribute 'read'
Run Code Online (Sandbox Code Playgroud)
还MyFile.py删除了所有内容.
任何人都可以告知出了什么问题.我读过ftp.storbinary用于使用FTP上传文件.
Joh*_*ohn 16
如果您尝试存储非二进制文件(如文本文件),请尝试将其设置为读取模式而不是写入模式.
ftp.storlines("STOR " + filename, open(filename, 'r'))
Run Code Online (Sandbox Code Playgroud)
对于二进制文件(任何无法在文本编辑器中打开的文件)打开您的读二进制模式
ftp.storbinary("STOR " + filename, open(filename, 'rb'))
Run Code Online (Sandbox Code Playgroud)
另外如果你计划使用ftp lib,你应该通过一个教程,我推荐来自effbot的这篇文章.
结合两个建议.最后的答案是
#!/usr/bin/python
import ftplib
import os
filename = "MyFile.py"
ftp = ftplib.FTP("xx.xx.xx.xx")
ftp.login("UID", "PSW")
ftp.cwd("/Unix/Folder/where/I/want/to/put/file")
os.chdir(r"\\windows\folder\which\has\file")
myfile = open(filename, 'r')
ftp.storlines('STOR ' + filename, myfile)
myfile.close()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
61027 次 |
| 最近记录: |