为什么递归ftp不会在这个目录中工作?

时间:2012-02-22 23:14:13

标签: python ftputil

Python newb ...小心!

我试图递归ftp一些文件。在下面的脚本中,我收到一个错误:

Traceback (most recent call last):
  File "dump.py", line 7, in <module>
    for root,dirs,files in recursive:
  File "/usr/local/lib/python2.6/site-packages/ftputil/ftputil.py", line 880, in walk
    if self.path.isdir(self.path.join(top, name)):
  File "/usr/local/lib/python2.6/site-packages/ftputil/ftp_path.py", line 133, in isdir
    path, _exception_for_missing_path=False)
  File "/usr/local/lib/python2.6/site-packages/ftputil/ftputil.py", line 860, in stat
    return self._stat._stat(path, _exception_for_missing_path)
  File "/usr/local/lib/python2.6/site-packages/ftputil/ftp_stat.py", line 624, in _stat
    _exception_for_missing_path)
  File "/usr/local/lib/python2.6/site-packages/ftputil/ftp_stat.py", line 578, in __call_with_parser_retry
    result = method(*args, **kwargs)
  File "/usr/local/lib/python2.6/site-packages/ftputil/ftp_stat.py", line 543, in _real_stat
    lstat_result = self._real_lstat(path, _exception_for_missing_path)
  File "/usr/local/lib/python2.6/site-packages/ftputil/ftp_stat.py", line 502, in _real_lstat
    for stat_result in self._stat_results_from_dir(dirname):
  File "/usr/local/lib/python2.6/site-packages/ftputil/ftp_stat.py", line 419, in _stat_results_from_dir
    lines = self._host_dir(path)
  File "/usr/local/lib/python2.6/site-packages/ftputil/ftp_stat.py", line 411, in _host_dir
    return self._host._dir(path)
  File "/usr/local/lib/python2.6/site-packages/ftputil/ftputil.py", line 811, in _dir
    descend_deeply=True)
  File "/usr/local/lib/python2.6/site-packages/ftputil/ftputil.py", line 578, in _robust_ftp_command
    self.chdir(path)
  File "/usr/local/lib/python2.6/site-packages/ftputil/ftputil.py", line 603, in chdir
    ftp_error._try_with_oserror(self._session.cwd, path)
  File "/usr/local/lib/python2.6/site-packages/ftputil/ftp_error.py", line 146, in _try_with_oserror
    raise PermanentError(*exc.args)
ftputil.ftp_error.PermanentError: 550 /usr/local/web: No such file or directory
Debugging info: ftputil 2.6, Python 2.6.5 (linux2)

Exited: 256

我不知道这意味着什么。但是,当我将目录更改为“/ path / dir2”时,它工作并打印出“file.txt”。

这是我的目录结构:

/path/dir1/another/file.txt # I get the above error with this path
/path/dir2/another/file.txt # this works fine, even though the directories have the same structure

我的剧本:     import ftputil

ftp = ftputil.FTPHost('ftp.site.com','user','pass')
recursive = ftp.walk("/path/dir1",topdown=True,onerror=None)
for root,dirs,files in recursive:
 for name in files:
  print name
ftp.close

1 个答案:

答案 0 :(得分:2)

/ path / dir1 /中是否存在指向/ usr / local / web的符号链接?

如果您想要跳过此错误,可以在其中输入try catch块并记录...

ftp = ftputil.FTPHost('ftp.site.com','user','pass')
try:
    recursive = ftp.walk("/path/dir1",topdown=True,onerror=None)
    for root,dirs,files in recursive:
         for name in files:
               print name
except Error e:
   print "Error: %s occurred" % (e)
ftp.close

以上将捕获所有错误。你也可以导入你得到的特定错误并执行此操作...

import ftputil.ftp_error.PermanentError as PermanentError
ftp = ftputil.FTPHost('ftp.site.com','user','pass')
try:
    recursive = ftp.walk("/path/dir1",topdown=True,onerror=None)
    for root,dirs,files in recursive:
         for name in files:
              print name
except PermanentError e:
   print "Permanent Error: %s occurred" % (e)
ftp.close