Python - Download Files from the directory

This is the place for queries that don't fit in any of the other categories.

Python - Download Files from the directory

Postby precyoz » Sun Aug 18, 2013 6:28 am

What should I do with this code so that it can download all the files from the directory? (not one by one).


Code: Select all
from ftplib import FTP
import sys, os, os.path, operator
import wx

def upload(handle,filename):
   f = open(filename,"rb")
   (base,ext) = os.path.splitext(filename)
   picext = ".bmp .jpg .jpeg .dib .tif .tiff .gif .png .hdf"
   if(operator.contains(picext,ext)):
      try:
         handle.storbinary("STOR " + filename,f,1)
      except Exception:
         print "Successful upload."
      else:
         print "Successful upload."
      f.close()
      return

   try:
      handle.storbinary("STOR " + filename,f)
   except Exception:
      print "Successful upload."
   else:
      print "Successful upload."
   f.close()
   return


def download(handle,filename):
   f2 = open(filename,"wb")
   try:
      handle.retrbinary("RETR " + filename,f2.write)
   except Exception:
      print "Error in downloading the remote file."
      return
   else:
      print "Successful download!"
   f2.close()
   return

print ""
host_name = raw_input("Enter your FTP Site: ")
if "http://" in host_name:
   host_name = host_name.replace("http://","")
host_name = host_name.replace("\n","")
user = raw_input("Enter username: ")
pwd = raw_input("Enter password: ")
try: ftph = FTP(host_name)
except:
   print "Host could not be resolved."
   raw_input()
   sys.exit()
else: pass
try:
   ftph.login(user,pwd)
except Exception:
   if user == "anonymous" or user == "Anonymous" and pwd == "anonymous" or pwd == "Anonymous":
      print "The server does not accept anonymous requests."
      raw_input()
      sys.exit()
   else:
      print "Invalid login combination."
      raw_input()
      sys.exit()
else:
   print "Successfully connected!\n"
Last edited by precyoz on Mon Aug 26, 2013 3:26 am, edited 2 times in total.
precyoz
 
Posts: 18
Joined: Wed Jul 03, 2013 6:23 am

Re: Python - Download Files from the directory

Postby hrs » Sun Aug 18, 2013 11:00 am

That's a big chunk of code. Maybe you can trim it down to about 10 lines that are specific to your problem. Then you can state what it does and what you want it to do.
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: Python - Download Files from the directory

Postby precyoz » Sun Aug 18, 2013 1:18 pm

The code was already edited. Every time I run the script you will be ask to fill the FTP site together with your username and password, and then it will automatically connect. You will use the 'get' command to download your file from the ftp server/directory. What I want is to download all the files from the directory(using 'get' or 'download' command). Downloading the files one by one is a waste of time. How am I going to do this?
precyoz
 
Posts: 18
Joined: Wed Jul 03, 2013 6:23 am

Re: Python - Download Files from the directory

Postby hrs » Sun Aug 18, 2013 5:49 pm

That's still a lot of code and it isn't all relevant to your question.

It seems that there's
Code: Select all
ftp.retrlines('LIST')

which lists the contents of the remote directory. You can then process that to get all the files in sequence.
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: Python - Download Files from the directory

Postby precyoz » Sun Aug 25, 2013 2:16 pm

I'm sorry because I'm only a newbie. Can you show me a sample script of downloading all the files from the directory instead?Because I really don't know how to do it :( and I don't have any idea :?:
precyoz
 
Posts: 18
Joined: Wed Jul 03, 2013 6:23 am

Re: Python - Download Files from the directory

Postby hrs » Sun Aug 25, 2013 2:37 pm

I think you should read some beginner tutorials for python. You should also read the ftplib documentation.

It seems you already have most of the code for downloading files. You need to get a list of files and download them in a loop or something like that. Example:
Code: Select all
>>> ftp = FTP('ftp.debian.org')
>>> ftp.login()
'230 Login successful.'
>>> ftp.retrlines('LIST')
drwxr-xr-x    8 1176     1176         4096 Aug 25 09:00 debian
'226 Directory send OK.'
>>> ftp.nlst()
['debian']
>>> ftp.dir()
drwxr-xr-x    8 1176     1176         4096 Aug 25 09:00 debian
>>> ftp.cwd('debian')
'250 Directory successfully changed.'
>>> ftp.pwd()
'/debian'
>>> ftp.nlst()
['README', 'README.CD-manufacture', 'README.html', 'README.mirrors.html', 'README.mirrors.txt', 'dists', 'doc', 'indices', 'ls-lR.gz', 'pool', 'project', 'tools']
>>> dir_listing = ftp.nlst()
>>> for item in dir_listing:
...   print item
...
README
README.CD-manufacture
README.html
README.mirrors.html
README.mirrors.txt                                                                                                                                         
dists                                                                                                                                                       
doc
indices
ls-lR.gz
pool
project
tools
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: Python - Download Files from the directory

Postby precyoz » Mon Aug 26, 2013 3:25 am

Thanks! I tried your sample script and it works. But then again, it only prints the directory list and the files, it doesn't download all the files. What should be the script to add for me to be able to download all those files from the directory?
precyoz
 
Posts: 18
Joined: Wed Jul 03, 2013 6:23 am


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest