简体   繁体   English

无法在Python中打开文件“无此文件或目录”错误

[英]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. 这个问题可能被问过几次,并相信我已经研究并找到了其中一些,但没有一个对我有用。

  1. open() in Python does not create a file if it doesn't exist 如果不存在,则Python中的open()不会创建文件
  2. ftplib file select ftplib文件选择

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.

相关问题 Python 无法打开文件(“目录”):没有这样的文件或目录 - Python cannot open file ('Directory'): No such file or directory Python,无法打开文件,错误 [2] 没有这样的文件或目录 - Python, unable to open file, Error [2] no such file or directory (Python)无法从目录打开文件 - (Python) cannot open the file from directory 在python中打开共享库文件时出错(OSError:无法打开共享库文件:没有此类文件或目录) - Error in opening shared object file in python ( OSError: cannot open shared object file: No such file or directory) 错误 libtorch_python.so:无法打开共享 object 文件:没有这样的文件或目录 - Error libtorch_python.so: cannot open shared object file: No such file or directory VS Code - python 无法打开扩展文件没有这样的文件或目录 - VS Code - python cannot open extention file no such file or directory 无法打开共享对象文件:在python的raspberry pi中没有这样的文件或目录 - cannot open shared object file: No such file or directory in raspberry pi with python I / O错误:从其他目录导入python文件时,无法打开资源 - I/O Error:Cannot open resource, while importing python file from the different directory 无法打开目录中的音频文件 - Cannot open audio file in the directory Python无法打开文件 - Python cannot open file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM