how to upload ziped file on server via ftp [duplicate]

女生的网名这么多〃 提交于 2019-12-08 09:12:57

问题


i have this code in python for upload photos.zip file on the server via ftp.

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()

but i have this error : 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'

also I used this solution :

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

but I get this error :

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.

回答1:


You have to escape the backslashes in the path:

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



回答2:


Use os.path.join which is considered better.

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

If you want to stick to your string, use \\ instead of \.



来源:https://stackoverflow.com/questions/56955895/how-to-upload-ziped-file-on-server-via-ftp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!