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
My first Python program!
Hey everyone, I just joined this board and look forward to being a part of the community here. Last night I was bored and decided I'd learn some Python, so I created my first program last night...so here it is. Don't be too hard on me, as this is my first program (I'm sure it could be optimized and could handle exceptions and input errors)... but anyways, the program is for downloading sequenced images from websites. Here's a sample of some input:
Quote:
URL of image folder:
(edit: gave wrong url before)
http://www.gamers-globe.com/images/screenshots/world-of-warcraft/
Image prefix:
world-of-warcraft-pc-0
First image number:
1
Last image number:
15
Leading 0 (this is for images 1-9 if for example image 1 is seen as 01) (y/n):
y
Extension:
jpg
The default download folder is C:\Images
if the folder doesn't already exist, the program creates it.
There is an error right now, where if there is a leading 0, it will download 10.jpg and 010.jpg
Also, the program will only work for less than 100 images if there is a leading 0.
If you have some ideas as to how to better automate the process, or maybe how to check whether the image actually exists on the server before the program tries to download it, or any other suggestions or tips for me please let me know!
Thanks!
Posts: 13
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
Quote:
Originally Posted by SuperOscar
A few further ideas:
you don’t need trailing semicolons in the lines
raw_input() can take a prompt parameter, you don’t need separate print statements
you can use os.mkdir() instead of tricky os.system() calls.
Thanks for letting me know about the semicolons, the Tutorial I looked at for the basics was in the Python interpreter so I figured that maybe for external scripts you needed the semicolons.
I switched to using prompts with raw_input() which made the code look nice and tidy.
BTW, what's the difference between raw_input and input?
Posts: 13
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
Updated code
I've made a few changes based on your recommendations. The code looks a lot neater, as does the console output. I've also fixed the problem of the script downloading 010.* and 10.* if there is a leading 0. I think I'll try to change it so that you can enter how many leading 0's there are instead of just being able to have 1, that way if there are leading 0's the script won't be limited to 99 images.
So, here's the updated code, tell me what you think or if you want to suggest some features to add, I'd appreciate it (mainly because I'd like to learn more, so implementing suggested features would be helpful for hands-on learning).
Code:
import urllib
import os
import random
os.chdir('c:\\')
if not os.path.exists(r'C:\\Images'):
os.mkdir(r'Images')
os.chdir(r'C:\\Images')
picture_page = raw_input('\nEnter URL of image folder: ')
img_prefix = raw_input('Enter image prefix: ')
img_fnum = raw_input('Enter starting image number: ')
img_lnum = raw_input('Enter last image number: ')
lead0 = raw_input('Leading 0 (y/n): ')
ext = raw_input('Enter file extension: ')
a = range(int(img_lnum))
for x in a:
if x < 9 and lead0 == 'y':
a[int(x)] = '0' + str(x+1)
else:
a[int(x)] = str(x+1)
rfolder = str(int(random.random()*7384))
os.mkdir(rfolder)
print('\n----Beginning download of ' + img_lnum + ' images----\n')
for x in a:
print('Src: ' + picture_page + img_prefix + str(a[int(x)-1]) + '.' + ext)
print('Dst: ' + os.getcwd() + '\\' + rfolder + '\\' + img_prefix + str(a[int(x)-1]) + '.' + ext)
urllib.urlretrieve(picture_page + img_prefix + str(a[int(x)-1]) + '.' + ext, os.getcwd() + '\\' + rfolder + '\\' + img_prefix + str(a[int(x)-1]) + '.' + ext)
print('----Download ' + x + '/' + img_lnum + ' complete----\n')
Posts: 1,075
Time spent in forums: 4 Weeks 1 Day 4 h 41 m 27 sec
Reputation Power: 98
I use python3 if I use python. Guido discarded the raw_input function. input() returns a string in python3. Then if you need to convert it to another type you convert explicitly.
Posts: 13
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
I tried changing
a = range(int(img_lnum)) to a = range(int(img_fnum), int(img_lnum))
but now the script gives an error of cannot concat 'str' and 'int ... I tried casting a[int(x)] in the for loop as a string, but still no luck... any ideas?