如何通过ftp在服务器上上传压缩文件

时间:2019-07-09 15:24:25

标签: python ftp upload

我在python中有此代码,可通过ftp在服务器上上传photos.zip文件。

import ftplib
session = ftplib.FTP('server.address.com','USERNAME','PASSWORD')
file = open('c:\archived\photos.zip','rb')                  # file to send
session.storbinary('STOR photos.zip', file)                 # send the file
file.close()                                                # close file and FTP
session.quit()

但是我有这个错误: T

raceback (most recent call last):
File "a.py", line 24, in <module>
file = open('c:\archived\photos.zip','rb')
IOError: [Errno 22] invalid mode ('rb') or filename: 'c:\archived\photos.zip'

我也使用了此解决方案:

file = open(os.path.join('c:/','archived','photos.zip'),'rb')

但是我得到这个错误:

Traceback (most recent call last):
  File "s.py", line 28, in <module>
    session.storbinary('s.zip', file)
  File "C:\Users\0xuser\Anaconda2\lib\ftplib.py", line 479, in storbinary
    conn = self.transfercmd(cmd, rest)
  File "C:\Users\0xuser\Anaconda2\lib\ftplib.py", line 378, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "C:\Users\0xuser\Anaconda2\lib\ftplib.py", line 341, in ntransfercmd
    resp = self.sendcmd(cmd)
  File "C:\Users\0xuser\Anaconda2\lib\ftplib.py", line 251, in sendcmd
    return self.getresp()
  File "C:\Users\0xuser\Anaconda2\lib\ftplib.py", line 226, in getresp
    raise error_perm, resp
ftplib.error_perm: 500 Unknown command.

2 个答案:

答案 0 :(得分:0)

您必须在路径中使用反斜杠:

file = open('c:\\archived\\photos.zip','rb')

答案 1 :(得分:0)

使用更好的os.path.join

file = open(os.path.join('c:/','archived','photos.zip'),'rb')

如果要坚持使用字符串,请使用\\而不是\