Python

Moderators: None (Apply to moderate this forum)
Number of threads: 474
Number of posts: 1166

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
My first Python program Posted by ViralFrost on 28 Jan 2012 at 10:30 AM
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:

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


The default download folder is C:\Images\
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:

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);



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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.