0

I want to log into a ftp server (not a public url) and download a csv file which is located in a zip file and then save this to a particular directory:

#log in OK

# this is the zip file I want to download
fpath = strDate + ".zip"

#set where to save file
ExtDir = "A:\\LOCAL\\DIREC\\TORY\\"""
ExtDir = ExtDir + strdate + "\\"
ExtFile = ExtDir + "Download.zip"

#download files
#use zipfile.ZipFile as alternative method to open(ExtFile, 'w')
with zipfile.ZipFile(ExtFile,'w') as outzip:
ftp.retrbinary('RETR %s' % fpath , outzip.write)
outzip.close

I get this error

File "C:\Program Files (x86)\Python 2.7\lib\ftplib.py", line 419, in retrbinary callback(data) File "C:\Program Files (x86)\Python 2.7\lib\zipfile.py", line 1123, in write st = os.stat(filename) TypeError: stat() argument 1 must be encoded string without null bytes, not str

REdim.Learning
  • 655
  • 2
  • 14
  • 32
  • Could you try with `open(ExtFile, 'wb')` ? See http://stackoverflow.com/a/2665873 – David Duponchel Dec 09 '16 at 20:01
  • it just gives me an error "IOError: [Errno 2] No such file or directory" As this 'wb' doesn't make a file. If I use `open(ExtFile, 'w')` and then close it afterwards, I get a zip file created but I can't open it. – REdim.Learning Dec 12 '16 at 09:52

1 Answers1

0

Fixed using:

ftp.retrlines('RETR %s' % fpath ,lambda s, w=outzip.write: w(s+"\n"))
REdim.Learning
  • 655
  • 2
  • 14
  • 32