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

    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:

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

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

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

  2. #2
    Join Date
    Jan 2012

    Re: My first Python program!

    Once again, here's the updated code with a couple of changes. Putting in a beginning image number actually works now, added comments, there can be infinite number of leading 0s with no limit on number of files to be downloaded **EDIT** - there is still a problem where you can only download files under 100...any ideas how to fix this? - **EDIT**
    **EDIT** - code updated, no problem with leading 0s or limit on file numbers or amount of files - **EDIT**, also changed code to reflect the ability to download any type of sequenced file.

    Save directory: C:\Downloads\
    URL of file folder on server: http://blob.perl.org/books/beginning-perl/
    File prefix: 3145_Chap
    Starting file number: 1
    Ending file number: 14
    Leading 0s: 1
    File extension: pdf


    import urllib
    import os
    import random

    #get input - local save directory
    savdir = raw_input('\nEnter the directory to save to: ')

    #if local save folder doesn't exist, create it - change directory to local save directory
    if not os.path.exists(savdir):

    #get input - URL, file prefix, first file number, last file number, number of leading 0s, file extension
    file_folder = raw_input('Enter URL of containing folder: ')
    fprefix = raw_input('Enter file prefix: ')
    ffnum = raw_input('Enter starting file number: ')
    flnum = raw_input('Enter ending file number: ')
    num0 = raw_input('Number of leading 0s (on ones column ex: 001 would be 2 0s, 01 would be 1 0): ')
    ext = raw_input('Enter file extension: ')

    #create list of file numbers - add leading 0s
    fmt = '{0}'
    fmt = '{0:0' + str(int(num0)+1) + 'd}'
    a = [fmt.format(x) for x in range(int(ffnum), int(flnum) + 1)]

    #create random folder in local save directory to save files to
    rfolder = str(int(random.random()*7384))

    #output file source and file destination - download files from URL and save to local save folder - output number of files downloaded / number of files
    print('\n----Beginning download of ' + str(int(flnum) - int(ffnum) + 1) + ' files----\n')
    for x in a:
    print('Src: ' + file_folder + fprefix + str(a[int(x) - int(ffnum)]) + '.' + ext)
    print('Dst: ' + os.getcwd() + '\\' + rfolder + '\\' + fprefix + str(a[int(x) - int(ffnum)]) + '.' + ext)
    urllib.urlretrieve(file_folder + fprefix + str(a[int(x) - 1 - int(ffnum)]) + '.' + ext, os.getcwd() + '\\' + rfolder + '\\' + fprefix + str(a[int(x) - int(ffnum)]) + '.' + ext)
    print('----Download ' + str(int(x) + 1 - int(ffnum)) + '/' + str(int(flnum) - int(ffnum) + 1) + ' complete----\n')

  3. #3
    Join Date
    Jan 2006

    Re: My first Python program!

    Instead of multiple concatenation:
    print('Src: ' + file_folder + fprefix + str(a[int(x) - int(ffnum)]) + '.' + ext)
    Make use of string formatting, e.g.,
    print('Src: {folder}{prefix}{num}.{ext}'.format(folder=file_folder,
                                                    num=a[int(x) - int(ffnum)],
    Actually, the whole business of computing the index for a looks unnecessary: isn't x already what you want? In fact, you don't even need a: you can loop through the range directly and then make use of string formatting within the loop.

    With regards to style, read PEP 8.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Jan 2012

    Re: My first Python program!

    Thanks for the suggestions, implementing new stuff is the best way to learn, for me. I'm just getting used to the 'for' loop style in Python and keep mixing up x being an iterator instead of the actual data. The link looks interesting, I'll check it out.

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.