如何将文件从FTP文件夹移动并替换到同一FTP中的另一个文件夹

Dus*_*ush 1 python ftp

我是Python的新手.我试图将ftp位置中的一些xml文件移动到同一个ftp中的另一个位置.我尝试使用以下代码,但它不起作用.

def ftpPush(filepathSource, filename, filepathDestination):
    try:
        ftp = FTP(ip, username, password)
        ftp.cwd(filepathDestination)

        ftp.storlines("STOR "+filename, open(filepathSource, 'r')) 
        ftp.quit()

        for fileName in os.listdir(path):
            if fileName.endswith(".xml"):
                ftpPush(filepathSource, filename, filepathDestination)

    except Exception, e:
        print str(e)

    finally:
        ftp.close()
Run Code Online (Sandbox Code Playgroud)

Mar*_*ryl 6

要移动文件,请使用FTP.rename.

假设filepathSourcefilepathDestination均为远程文件,你这样做:

ftp.rename(filepathSource, filepathDestination)
Run Code Online (Sandbox Code Playgroud)