Python FTP Only downloads 16 files not all files

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

Python FTP Only downloads 16 files not all files

Postby precyoz » Sat Jan 04, 2014 3:03 pm

Hello everyone. I'm encountering a problem with my python ftp download. Every time I run this script and connect to my ftp site it only downloads 16 files wherein the ftp site contains almost hundred files and counting because it always update and add files everyday. I really can't figure out what's missing in my script to download all those files. Any help/suggestions/comments will be appreciated. Thanks!

Here is the script:

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

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"
print ftph.getwelcome()
flag = 1
count = 0
path = ftph.pwd()
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

def handleDownload(block):
    file.write(block)
    print ".",

ddir='#localdirectory'
os.chdir(ddir)
ftp = FTP('#yourftpsite')
print 'Logged in!'

ftp.login('', '')
directory = '#ftpdirectory'

print 'Changing to ' + directory
ftp.cwd(directory)
ftp.retrlines('LIST')
print 'Downloading files...'

filenames = []
ftp.retrlines('NLST', filenames.append)
print filenames

for filename in filenames:
    local_filename = os.path.join('#localdirectory', filename)
    file = open(local_filename, 'wb')
    ftp.retrbinary('RETR '+ filename, file.write)

    file.close()
   
ftp.close()
precyoz
 
Posts: 18
Joined: Wed Jul 03, 2013 6:23 am

Re: Python FTP Only downloads 16 files not all files

Postby micseydel » Thu Jan 09, 2014 6:44 pm

Your code is difficult to read and not well structured, so it's difficult to know where to start here. If you check the value of filenames before the loop, is it just 16 file names long?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1443
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Python FTP Only downloads 16 files not all files

Postby tnknepp » Thu Jan 09, 2014 7:10 pm

I do not really understand your method. Why not use FTP.nlst to retrieve a list of filenames on the ftp, then iterate through them to download?
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 134
Joined: Mon Mar 11, 2013 7:41 pm


Return to General Coding Help

Who is online

Users browsing this forum: Crimson King, mdovey, mk35 and 4 guests