ftplib中没有这样的文件或目录?

时间:2014-02-25 07:00:10

标签: python python-2.7 ftp

所以,我正在尝试为平板电脑和其他计算机上的文件编写同步程序。不幸的是,我只是熟悉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)

在“打开...”的行上,我收到此错误:

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端。我做错了吗?是否有不同的功能可以远程打开此文件?

1 个答案:

答案 0 :(得分:1)

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

替换以下行:

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

使用:

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