【问题标题】:No such file or directory in ftplib?ftplib 中没有这样的文件或目录?
【发布时间】:2014-02-25 07:00:09
【问题描述】:

所以,我正在尝试为我的平板电脑和其他计算机上的文件编写一个同步程序。不幸的是,我只对 python 和 ftplib 相当熟悉。代码如下:

#DOWNLOADING
for file in rContents:
    if rIsFolder(ftp, file):
        rFolders.append(file)
        continue
    if file in contents:
        print "%s already uploaded." % file
    else:
        try:
            print ftp.pwd()
            print "Downloading %s..." % file
            with open(file, 'rb') as f:        
                ftp.retrbinary('RETR %s' % file, f.write)
            print "    ...done."
        except:
            print "Failed download of %s." % file

我特别关心这两行:

with open(file, 'rb') as f:        
            ftp.retrbinary('RETR %s' % file, f.write)

在 "with open..." 行我收到此错误:

Traceback (most recent call last):
  File "FolderSync.py", line 145, in <module>
    sync(ftp, folder, tabFolder)
  File "FolderSync.py", line 67, in sync
    with open(file, 'rb') as f:        
IOError: [Errno 2] No such file or directory: 'u.txt'

我实际上是使用 ftp.nlst() 来获取这些文件名,所以 'u.txt' 绝对存在,但在 ftp 方面。我做错了吗?远程打开这个文件有不同的功能吗?

【问题讨论】:

    标签: python python-2.7 ftp


    【解决方案1】:

    要检索文件,您需要以写入模式打开文件 (wb)。

    替换以下行:

    with open(file, 'rb') as f:        
    

    与:

    with open(file, 'wb') as f:        
    

    【讨论】:

    • 谢谢,成功了!你能帮我解决这个问题吗? OpenedFile = open(file, 'rb') ftp.storbinary('STOR' + file, opensFile) 这些是我用来上传的行,但它们也不起作用。编辑:格式化在这里不起作用。我会把它作为编辑放入。
    • 啊,我明白了。谢谢您的帮助。上传行的问题是我缺少一个空格。傻我。
    猜你喜欢
    • 2021-06-24
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 2011-03-25
    • 1970-01-01
    相关资源
    最近更新 更多