[英]Cannot open file in Python “No such file or directory” error
I've been trying to download files from an FTP server. 我一直在尝试从FTP服务器下载文件。 For this I've found this Python-FTP download all files in directory and examined it. 为此,我找到了该Python-FTP下载目录中的所有文件并进行了检查。 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. 我尝试了w+
, a+
, rw
等,并且我一直都在遇到相同的错误。 Any ideas? 有任何想法吗?
Note: I am using OSX Mavericks and Python 2.7.5. 注意:我正在使用OSX Mavericks和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. 这个问题可能被问过几次,并相信我已经研究并找到了其中一些,但没有一个对我有用。
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? nlst()返回完整路径还是仅返回文件名? 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. 如果它的文件名不是retrbinary,可能是“ / Foo / file”而是“ file”,则服务器的根目录中可能没有任何名为file的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.