简体   繁体   中英

unable to download files recursively using ftputil

I am trying to write a code which should download the files which have been created in the past 6 days. I am able to print the files but not able to download. Please suggest where i am wrong and help me complete the sript.

import ftplib
import ftputil
import os
import datetime

now=datetime.datetime.now()
print (now)
ago=now-datetime.timedelta(days=6)
print (ago)

class MySession(ftplib.FTP):
    def __init__(self, host, userid, password, port):
         ftplib.FTP.__init__(self)
         self.connect(host, port)
         self.login(userid, password)
ftp = ftputil.FTPHost('host', 'user', 'pwd', port=21,
                 session_factory=MySession)


dir_dest=os.chdir('C:/Python34/New folder')

for root,dirs,files in ftp.walk('Windows Triage' , topdown=True):
    for name in files:
        path=ftp.path.join(root,name)
        st=ftp.stat(path)
        ctime=datetime.datetime.fromtimestamp(st.st_mtime)
        if ctime>ago:
            print(name)
            for fname in name:
                fpath = ftp.path.join(root,fname)
                if ftp.path.isfile(fpath):
                    ftp.download(fpath,os.path.join(dir_dest, fname), 'b')

It looks like you are not recursively descending into the directories.

Enclose your code in a function

def f(dirpath):
    for root,dirs,files in ftp.walk(dirpath , topdown=True):
        for name in files:
            path=ftp.path.join(root,name)
            st=ftp.stat(path)
            ctime=datetime.datetime.fromtimestamp(st.st_mtime)
            if ctime>ago:
                print(name)
                for fname in name:
                    fpath = ftp.path.join(root,fname)
                    if ftp.path.isfile(fpath):
                        ftp.download(fpath,os.path.join(dir_dest, fname), 'b')
        for curdir in dirs:
            f(os.path.join(root, curdir))

and call it initially with

f('Windows Triage')

Mind you: I have not tried it and I have not used the python ftp library yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM