My first Python program - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

My first Python program

ViralFrostViralFrost Posts: 3Member
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:

[color=Blue]URL of image folder:
http://www.gamersglobe.co.uk/screenshot/

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[/color]

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;

os.chdir('c:\');

if os.path.exists('C:\Images'):
os.chdir('C:\Images');
else:
os.system('mkdir Images');
os.chdir('C:\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);
else:
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!
Thanks!

Comments

Sign In or Register to comment.