简体   繁体   中英

Cannot open file in Python “No such file or directory” error

I've been trying to download files from an FTP server. For this I've found this Python-FTP download all files in directory and examined it. Anyways, I extracted the code I needed and it shows as follows:

import os

from ftplib import FTP

ftp = FTP("ftp.example.com", "exampleUsername", "examplePWD")

file_names = ftp.nlst("\public_html")

print file_names

for filename in file_names:
    if os.path.splitext(filename)[1] != "":
        local_filename = os.path.join(os.getcwd(), "Download", filename)
        local_file = open(filename, 'wb')
        ftp.retrbinary('RETR ' + filename, local_file.write)
        local_file.close()

ftp.close()

But when it tries to open the file, it keeps saying:

ftplib.error_perm: 550 Can't open CHANGELOG.php: No such file or directory

I've tried w+ , a+ , rw , etc. and I keep getting the same error all the time. Any ideas?

Note: I am using OSX Mavericks and Python 2.7.5.

This question may have been asked several times and believe me I researched and found some of them and none of them worked for me.

  1. open() in Python does not create a file if it doesn't exist
  2. ftplib file select

It looks like you are listing files in a directory and then getting files based on the returned strings. Does nlst() return full paths or just filenames? If its just filenames than retrbinary might be expecting "/Foo/file" but getting "file", and there might not be anything named file in the root dir of the server.

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