我試圖從windows服務器上傳文件到unix服務器(基本上試圖做FTP)。我用下面FTP上傳文件Python
#!/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)
代碼我收到以下錯誤:
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'
同樣的MyFile.py
所有內容刪除了。
任何人都可以建議發生了什麼問題。我已經讀了ftp.storbinary用於使用FTP上傳文件。
MyFile.py的內容不會被刪除。但我仍然在等錯誤。 – misguided
使用storlines假定myfile是一個文本文件。對於其他文件(圖像,電影,檔案,基本上任何你無法在文本編輯器中打開並閱讀的內容),你應該使用storbinary並用'rb'打開文件,而不是'r –
根據http://bugs.python .org/issue6822非二進制文件也應該用'rb'標誌打開,但之後仍然用'storlines()'上傳。只是遇到了'r'和'Type str不支持緩衝區AP'的問題,並且提供了鏈接。 –