Page 2 - Discuss My first Python program! in the Python Programming forum on Dev Shed. My first Python program! Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.
Posts: 13
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
Ya, I've always been bad with adding comments to my code lol...I don't usually need it myself unless the program is really complex, so it's usually an after thought.
Posts: 13
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
Updated Code
Once again, here's the updated code with a couple of changes. Putting in a beginning image number actually works now, added comments, there can be infinite number of leading 0s with no limit on number of files to be downloaded **EDIT** - there is still a problem where you can only download files under 100...any ideas how to fix this? - **EDIT**
**EDIT** - code updated, no problem with leading 0s or limit on file numbers or amount of files - **EDIT**, also changed code to reflect the ability to download any type of sequenced file.
Quote:
Example:
Save directory: C:\Downloads\
URL of file folder on server: http://blob.perl.org/books/beginning-perl/
File prefix: 3145_Chap
Starting file number: 1
Ending file number: 14
Leading 0s: 1
File extension: pdf
Code:
import urllib
import os
import random
#get input - local save directory
savdir = raw_input('\nEnter the directory to save to: ')
#if local save folder doesn't exist, create it - change directory to local save directory
if not os.path.exists(savdir):
os.mkdir(savdir)
os.chdir(savdir)
#get input - URL, file prefix, first file number, last file number, number of leading 0s, file extension
file_folder = raw_input('Enter URL of containing folder: ')
fprefix = raw_input('Enter file prefix: ')
ffnum = raw_input('Enter starting file number: ')
flnum = raw_input('Enter ending file number: ')
num0 = raw_input('Number of leading 0s (on ones column ex: 001 would be 2 0s, 01 would be 1 0): ')
ext = raw_input('Enter file extension: ')
#create list of file numbers - add leading 0s
fmt = '{0}'
fmt = '{0:0' + str(int(num0)+1) + 'd}'
a = [fmt.format(x) for x in range(int(ffnum), int(flnum) + 1)]
#create random folder in local save directory to save files to
rfolder = str(int(random.random()*7384))
os.mkdir(rfolder)
#output file source and file destination - download files from URL and save to local save folder - output number of files downloaded / number of files
print('\n----Beginning download of ' + str(int(flnum) - int(ffnum) + 1) + ' files----\n')
for x in a:
print('Src: ' + file_folder + fprefix + str(a[int(x) - int(ffnum)]) + '.' + ext)
print('Dst: ' + os.getcwd() + '\\' + rfolder + '\\' + fprefix + str(a[int(x) - int(ffnum)]) + '.' + ext)
urllib.urlretrieve(file_folder + fprefix + str(a[int(x) - 1 - int(ffnum)]) + '.' + ext, os.getcwd() + '\\' + rfolder + '\\' + fprefix + str(a[int(x) - int(ffnum)]) + '.' + ext)
print('----Download ' + str(int(x) + 1 - int(ffnum)) + '/' + str(int(flnum) - int(ffnum) + 1) + ' complete----\n')