我正在尝试将文件上传到FTP服务器,我想编写一个Python脚本来简化流程。目前我ftp到服务器并执行这些命令:
quote allo file_size_in_bytes
put c:\path\to\file
这是我到目前为止,我无法使用put
命令传输文件。
from ftplib import FTP
import os
import time
from subprocess import call
ip = raw_input('Enter ip address: ') # User input for host
print ip # Prints host
filelocation = raw_input('Drag file here: ') # File to upload
print ('This is the local file '+filelocation) # Verify file location
filename = os.path.basename(filelocation) # If a File name is needed
filesize = os.path.getsize(filelocation) # Need file size in bytes for quote allo command
print ('This is the file size in bytes '+str(filesize)) # Verify correct size
time.sleep(2) # Pause to look at screen
ftp = FTP(ip) # Open ftp connection
ftp.login('user','pass') # Login
print ftp.getwelcome() # Verify proper connection
remotepath = ftp.pwd()+filename # If a remote path to ftp server is needed
print ('This is the file path on the processor '+remotepath) # Verify remote path
"\"quote\"" # Need this, not sure why
ftp.sendcmd('allo '+str(filesize)) # quote allo filesize, seems to work
#"\"put\"" # Experimenting, don't know if this is needed
call(['echo "put C:\filelocation" | ftp']) # This doesn't appear to work
time.sleep(5)
ftp.quit()
答案 0 :(得分:0)
您正在使用ftplib登录,但是您正在尝试为上传本身运行外部ftp
进程。这不起作用,因为外部ftp
进程不知道您的ftplib FTP会话。
要使用ftplib上传文件,请使用storbinary
或storlines
方法。