Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming
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.

Learn More!


Download to Enter
| Contest Rules

Tutorials | Forums

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #16  
Old January 29th, 2012, 03:33 PM
ViralFrost ViralFrost is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 ViralFrost User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #17  
Old January 29th, 2012, 03:38 PM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 285 SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 4 h 28 m 58 sec
Reputation Power: 16
Quote:
Originally Posted by ViralFrost
I don't understand what you're doing with fmt ( '{0}' or '{0:02d}') please explain


Doing (new-style) string formatting. The following:

Code:
num = 2
print('{0}'.format(num))


prints just “2”. “{0}” is the placeholder for the argument of the format() method.

The following:

Code:
print('{0:02d}'.format(num))


would print “02”; the formatting string “02d” means a 2-digit number padded with zeroes.

Note: you can use the format() method anywhere, not only in a print statement (or print call, as in my examples).

Reply With Quote
  #18  
Old January 29th, 2012, 04:14 PM
ViralFrost ViralFrost is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 ViralFrost User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
I think one of my mistakes was thinking that in 'for x in a', x was in iterator, but I just realized that it's the data in a[...]... is that correct?

Reply With Quote
  #19  
Old January 29th, 2012, 04:16 PM
ViralFrost ViralFrost is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 ViralFrost User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
I've got a lot to learn about Python clearly, but I really appreciate all the help, thank you all very much!

Reply With Quote
  #20  
Old January 29th, 2012, 05:10 PM
ViralFrost ViralFrost is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 ViralFrost User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
Lightbulb 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')

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > My first Python program!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap