简体   繁体   中英

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

Hi I try to build a script that Downloads all files from an ftp Server to my harddisk and delets them on the ftpfolder.

here is the code.

from ftplib import FTP
import os
ftp = FTP('ftp.server.xxx')
ftp.login(user='user', passwd = 'pass')
ftp.cwd('/subfolder1/')
ftp.retrlines('LIST')
filenames = ftp.nlst()
print filenames

for filename in filenames:
    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 'Finished'

the Problem is that I get this error. The Folder "D:\\Temp" exists

Traceback (most recent call last):
  File "C:/Python27/test2.py", line 12, in <module>
    file = open(local_filename, 'wb')
IOError: [Errno 13] Permission denied: 'D:\\test\\..'

The filename it is trying to access is .. , which is "one directory up". You are trying to create a file called D:\\\\test\\\\.. , which is actually D:\\\\ . You can't create such a file, so you are getting a "Permission Denied" error.

ftp.nlst() operates similar to the Unix ls command, in that it returns two "implied files":

  • .. : the parent directory
  • . : the current directory

You will likely want to update your code to filter these out.

from ftplib import FTP
import os
ftp = FTP('ftp.server.xxx')
ftp.login(user='user', passwd = 'pass')
ftp.cwd('/subfolder1/')
ftp.retrlines('LIST')
filenames = ftp.nlst()
print filenames

for filename in filenames:
    if filename not in ['..', '.']:
        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 'Finished'

The file listing inclide .. for the parent folder. You have to skip that. D:\\test\\.. is D:\\ .

Thanks it is working a Little bit more.

I try to copy the files 1.txt to 8.txt It creates 1.txtin "D:\\test" now but there is nothing in the file

drwxr-x---   9 ftp      ftp          4096 Mar 17 17:32 ..
-rw-r--r--   1 ftp      ftp             7 Mar 17 17:37 1.txt
-rw-r--r--   1 ftp      ftp             7 Mar 17 17:37 2.txt
-rw-r--r--   1 ftp      ftp             7 Mar 17 17:37 3.txt
-rw-r--r--   1 ftp      ftp             7 Mar 17 17:37 4.txt
-rw-r--r--   1 ftp      ftp             7 Mar 17 17:37 5.txt
-rw-r--r--   1 ftp      ftp             7 Mar 17 17:37 6.txt
-rw-r--r--   1 ftp      ftp             7 Mar 17 17:37 7.txt
-rw-r--r--   1 ftp      ftp             7 Mar 17 17:37 8.txt
['..', '1.txt', '2.txt', '3.txt', '4.txt', '5.txt', '6.txt', '7.txt', '8.txt']

Traceback (most recent call last):
  File "C:/Python27/test2.py", line 14, in <module>
    ftp.retrbinary('RETR '+ filename, file.write)
  File "C:\Python27\lib\ftplib.py", line 398, in retrbinary
    self.voidcmd('TYPE I')
  File "C:\Python27\lib\ftplib.py", line 248, in voidcmd
    self.putcmd(cmd)
  File "C:\Python27\lib\ftplib.py", line 178, in putcmd
    self.putline(line)
  File "C:\Python27\lib\ftplib.py", line 173, in putline
    self.sock.sendall(line)
AttributeError: 'NoneType' object has no attribute 'sendall'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM