2012-02-22 75 views
1

Python newb ...小心!爲什麼不會遞歸ftp在這個目錄下工作?

我想遞歸地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 

我的腳本: 進口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 

回答

2

是否有/路徑symblolic鏈接/ DIR1 /回指向在/ usr /本地/網絡?

如果要執行這個錯誤,你可以把一個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 
+0

這可能是它,但我真的不知道,因爲該FTP站點是第三方。如果是這樣,我該如何「跳過」具有符號鏈接的文件? – usr951 2012-02-22 23:28:28

+0

請參閱使用捕捉錯誤並移動 – 2012-02-22 23:35:30

+0

thx的方式編輯。它在錯誤之後不會繼續。它會打印錯誤一次,然後死 – usr951 2012-02-22 23:52:44