2015-10-06 35 views
0

通過FTP發送的文件我曾嘗試下面的代碼,並改變了所有可能的方式像無法使用Python 2.6

storbinarystorlines[RRBRB +但即使沒有運氣將文件傳輸到服務器。下面是我的示例代碼:

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" 

的代碼只是運行和輸出的所有打印語句,但是當我在服務器檢查有沒有文件created.Consider登錄成功完成。

需要建議以實現所需的輸出。謝謝

+0

您應該檢查的'storebinary' – mata

+0

到底是什麼'inputfile'反應? –

+0

@BurhanKhalid輸入文件是我本地系統中的文本文件。例如inputfile ='c:\ working \ cyborg.txt' – cyborg

回答

0

你沒有登錄,所以你什麼都做不了。你確定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() 
+0

記錄成功完成,我已經更新了我的問題 – cyborg

+0

所以它仍然不適合我的代碼嗎? – Noxeus

+0

請將您的代碼與問題中給出的代碼進行比較,基本上兩者都是相同的,並且它尚未運行。 – cyborg

0

您需要通過STOR the filename on the remote server,因爲就是你正在傳遞一個路徑。

您還需要使用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() 
+0

請在字符串格式中加0。例如ftp.storlines('STOR {0}'.format(remote_file_name) – cyborg