Sunday, 15 August 2010

Python: ftplib and os Errno13 (download all files from ftp server) -


Hi, I try to make a script that downloads all the files from an FTP server to all files and removes them to the firstfolder.

Here is the code. Ftplib Import From FTP Import OS ftp = FTP ('ftp.server.xxx') ftp.login (user = 'user', passwd = 'pass') ftp.cwd (' / subfolder1 / ') ftp.retrlines (' LIST ') File name = ftp.nlst () for file names for file names print: local_filename = os.path.join (' D: \\ Test \\ ', filename) file = open (local_filename,' wb ' ) Ftp.retrbinary ('RETR' + filename, file.write) ## Here I want to delete the file and then switch to the next file ftp.quit () print 'end'

The problem is that I get this error folder "D: \ temp" exists

  traceback (most recent Call final): File "C: / Python27 / test2.py", line 12, & lt; Module & gt; File = open (local_filname, 'wb') IOError: [Error 13] Permission denied: 'D: \\ test \\ ..'  

This is the file name , it's an attempt to access .. , which is" a directory up ". You are trying to create a file named D: \\ test \\ .. , which is actually D: \\ . You can not create such a file, so you are getting the "Permission denied" error.

ftp.nlst () Unix ls command gives two "contained files" in it:

  • < Code> .. : parent directory
  • . : current directory

You probably want to update your code to filter them. Import from

  import ftplib import FTP import os ftp = FTP ('Ftp.server.xxx') ftp.login (user = 'user', passwd = 'pass') ftp.cwd ( '/ Subfolder1 /') ftp.retrlines ('list') file name = ftp.nlst () file name for file name in print file names: if the file name is not in ['..', '.'] : Local_filname = os.path.join ('D: \\ test \\', filename) file = open (local_filename, 'wb') Ftp.retrbinary ('RETR' + file name, file.write) # # I I want to delete the file and then switch to the next file ftp.quit () print 'end'  

No comments:

Post a Comment