CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Jan 2012
    Posts
    3

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

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

    Anyways, here's the code:

    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 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!
    Last edited by Cimperiali; January 28th, 2012 at 05:02 PM. Reason: adding Code tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured