0

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.
erikson
  • 1
  • 2

2 Answers2

0

You have to escape the backslashes in the path:

file = open('c:\\archived\\photos.zip','rb')
Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
0

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 \.

DanielM
  • 2,604
  • 5
  • 28
  • 41
  • this error:>>> Traceback (most recent call last): File "tgs.py", line 27, in file = open(os.path.join('c:','archived','photos.zip'),'rb') IOError: [Errno 2] No such file or directory: 'c:archived\\photos.zip' – erikson Jul 09 '19 at 16:10
  • fixed, it should be `c:/` – DanielM Jul 09 '19 at 16:23
  • That is a different question.. please post for it a different question – DanielM Jul 10 '19 at 12:39