4

FTP 経由でアドレスに接続し、すべてのコンテンツを削除したい。現在、私はこのコードを使用しています:

from ftplib import FTP
import shutil
import os

ftp = FTP('xxx.xxx.xxx.xxx')
ftp.login("admin", "admin")

for ftpfile in ftp.nlst():
    if os.path.isdir(ftpfile)== True:
        shutil.rmtree(ftpfile)
    else:
        os.remove(ftpfile)

私の問題は、彼が最初のファイルを削除しようとすると、常にこのエラーが発生することです。

    os.remove(ftpfile)
WindowsError: [Error 2] The system cannot find the file specified: somefile.sys

誰にも理由がありますか?

4

5 に答える 5

6
for something in ftp.nlst():
    try:
        ftp.delete(something)
    except Exception:
        ftp.rmd(something)

他の方法はありますか?

于 2012-04-06T14:17:46.343 に答える
0
ftp.nlst()

上記のステートメントは、ファイル名のリストを返します。

os.remove()

上記のステートメントにはファイルパスが必要です。

于 2012-04-06T11:58:55.413 に答える