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)
   print "Host could not be resolved."
else: pass
except Exception:
   if user == "anonymous" or user == "Anonymous" and pwd == "anonymous" or pwd == "Anonymous":
      print "The server does not accept anonymous requests."
      print "Invalid login combination."
   print "Successfully connected!\n"
print ftph.getwelcome()
flag = 1
count = 0
path = ftph.pwd()
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

def handleDownload(block):
    print ".",

ftp = FTP('#yourftpsite')
print 'Logged in!'

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

print 'Changing to ' + directory
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)

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!

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
Posts: 1614
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
Posts: 151
Joined: Mon Mar 11, 2013 7:41 pm

Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], snippsat, stapler8, Yoriz and 2 guests