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 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:

[color=Blue]URL of image folder:

Image prefix:

First image number:

Last image number:

Leading 0 (this is for images 1-9 if for example image 1 is seen as 01) (y/n):


The default download folder is [color=Green]C:Images[/color]
if the folder doesn't already exists, 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.

Anyways, here's the code:

[color=Orange]import urllib;
import os;
import random;


if os.path.exists('C:\Images'):
os.system('mkdir Images');

print('Enter URL of image folder: ');
picture_page = raw_input();

print('Enter image prefix: ');
img_prefix = raw_input();

print('Enter starting image number: ');
img_fnum = raw_input();

print('Enter last image number: ');
img_lnum = raw_input();

print('Leading 0 (y/n): ');
lead0 = raw_input();

print('Enter file extension: ');
ext = raw_input();

a = range(int(img_lnum));

for x in a:
if lead0 == 'y':
if x < 10:
a[int(x)] = '0' + str(x+1);
a[int(x)] = str(x+1);

rfolder = random.random();
rfolder = rfolder * 7397;
rfolder = str(int(rfolder));
os.system('mkdir ' + rfolder);

for x in a:
print('Source: ' + picture_page + img_prefix + str(a[int(x)-1]) + '.' + ext);
print('Destination: ' + 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);[/color]

If you have any suggestions or tips please let me know!


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion