0

I am trying to download files which have been created in past 6 days, I'm able to print the names of the files but not download the files from the FTPserver to the local system. Please suggest where I'm wrong.

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')
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • So... what happens instead? – jonrsharpe Aug 05 '15 at 10:40
  • What value do you think is bound to `fname` in `for fname in name:` in each loop iteration? Check your assumption by `print`ing it. And what's the value of `dir_dest`? – BlackJack Aug 05 '15 at 11:11
  • if i give print, i will get one file name per iteration, how can i specify this file to download, 'dir_dest' is the target directory where i want the files to get downloaded – Yathish Srinivas Aug 05 '15 at 12:38
  • Please `print` out `fname`. You won't get a _name_ per iteration then. – BlackJack Aug 06 '15 at 15:38
  • think about the difference `for character in "some string"` and `for s in ["some", "list"]` – jfs Aug 06 '15 at 20:06
  • unrelated: here's [code that downloads all files in a folder using `paramiko` (sftp)](http://stackoverflow.com/a/20381739/4279) – jfs Aug 06 '15 at 20:10
  • hello guys, i figured it out and it is working properly. Instead of taking "fname in name", i took "name in files" – Yathish Srinivas Aug 07 '15 at 11:17

0 Answers0