Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.

Learn More!


Download to Enter
| Contest Rules

Tutorials | Forums

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 28th, 2012, 01:41 PM
ViralFrost ViralFrost is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 ViralFrost User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
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:

Quote:
URL of image folder:
(edit: gave wrong url before)
http://www.gamers-globe.com/images/screenshots/world-of-warcraft/

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:

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!

Reply With Quote
  #2  
Old January 28th, 2012, 10:07 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Click here for more information.
 
Join Date: Aug 2011
Posts: 1,075 b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 4 h 41 m 27 sec
Reputation Power: 98
A thought

Code:
if not os.path.exists(r'C:\Images'):
	os.system(r'mkdir C:\Images')
os.chdir(r'C:\Images')

new line terminates statements.
The program always changes directory, do not duplicate code.

Reply With Quote
  #3  
Old January 28th, 2012, 10:29 PM
ViralFrost ViralFrost is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 ViralFrost User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
Good call, I tried using if !os.path.exists('...'): ...
but it didn't work, good to know that I can use 'not'

Reply With Quote
  #4  
Old January 29th, 2012, 03:48 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 285 SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 4 h 28 m 58 sec
Reputation Power: 16
Some ideas

A few further ideas:
  • you don’t need trailing semicolons in the lines
  • raw_input() can take a prompt parameter, you don’t need separate print statements
  • you can use os.mkdir() instead of tricky os.system() calls.

Reply With Quote
  #5  
Old January 29th, 2012, 01:01 PM
ViralFrost ViralFrost is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 ViralFrost User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
Quote:
Originally Posted by SuperOscar
A few further ideas:
  • you don’t need trailing semicolons in the lines
  • raw_input() can take a prompt parameter, you don’t need separate print statements
  • you can use os.mkdir() instead of tricky os.system() calls.


Thanks for letting me know about the semicolons, the Tutorial I looked at for the basics was in the Python interpreter so I figured that maybe for external scripts you needed the semicolons.

I switched to using prompts with raw_input() which made the code look nice and tidy.

BTW, what's the difference between raw_input and input?

Reply With Quote
  #6  
Old January 29th, 2012, 01:05 PM
ViralFrost ViralFrost is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 ViralFrost User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
what's the difference between os.chdir(r'C:\Images') and os.chdir('C:\Images') ?

Reply With Quote
  #7  
Old January 29th, 2012, 01:30 PM
ViralFrost ViralFrost is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 ViralFrost User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
Updated code

I've made a few changes based on your recommendations. The code looks a lot neater, as does the console output. I've also fixed the problem of the script downloading 010.* and 10.* if there is a leading 0. I think I'll try to change it so that you can enter how many leading 0's there are instead of just being able to have 1, that way if there are leading 0's the script won't be limited to 99 images.

So, here's the updated code, tell me what you think or if you want to suggest some features to add, I'd appreciate it (mainly because I'd like to learn more, so implementing suggested features would be helpful for hands-on learning).

Code:
import urllib
import os
import random

os.chdir('c:\\')

if not os.path.exists(r'C:\\Images'):
	os.mkdir(r'Images')
	
os.chdir(r'C:\\Images')

picture_page = raw_input('\nEnter URL of image folder: ')
img_prefix = raw_input('Enter image prefix: ')
img_fnum = raw_input('Enter starting image number: ')
img_lnum = raw_input('Enter last image number: ')
lead0 = raw_input('Leading 0 (y/n): ')
ext = raw_input('Enter file extension: ')

a = range(int(img_lnum))

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

rfolder = str(int(random.random()*7384))
os.mkdir(rfolder)

print('\n----Beginning download of ' + img_lnum + ' images----\n')
for x in a:
		print('Src: ' + picture_page + img_prefix + str(a[int(x)-1]) + '.' + ext)
		print('Dst: ' + 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)
		print('----Download ' + x + '/' + img_lnum + ' complete----\n')

Reply With Quote
  #8  
Old January 29th, 2012, 01:36 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Click here for more information.
 
Join Date: Aug 2011
Posts: 1,075 b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 4 h 41 m 27 sec
Reputation Power: 98
strings

python has many ways to write a string.

raw string retains backslash

'\\abc' == r'\abc'

unicode string
u'abc' == 'abc'

"abc" == 'abc'

'''abc''' == 'abc'

"""abc""" == 'abc'

You can answer this other question easily: ha ha I thought they are different. They are alike.
Code:
$ python
>>> ('C:\Images',r'C:\Images',)
('C:\\Images', 'C:\\Images')
>>> 'C:\Images'==r'C:\Images'
True

Reply With Quote
  #9  
Old January 29th, 2012, 01:38 PM
ViralFrost ViralFrost is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 ViralFrost User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
Post

Nevermind, answered my own question...doesn't work

---
Would I also be able to change a[int(x)] with x, or a[int(x)-1] with int(x)-1?

Last edited by ViralFrost : January 29th, 2012 at 01:55 PM. Reason: Answered my own question

Reply With Quote
  #10  
Old January 29th, 2012, 01:48 PM
ViralFrost ViralFrost is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 ViralFrost User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
Okay, so if using slashes in the input, like for web addresses or directories, I would have to use raw_input(), right?

Reply With Quote
  #11  
Old January 29th, 2012, 02:07 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Click here for more information.
 
Join Date: Aug 2011
Posts: 1,075 b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 4 h 41 m 27 sec
Reputation Power: 98
I use python3 if I use python. Guido discarded the raw_input function. input() returns a string in python3. Then if you need to convert it to another type you convert explicitly.

Reply With Quote
  #12  
Old January 29th, 2012, 02:16 PM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 285 SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 4 h 28 m 58 sec
Reputation Power: 16
Quote:
Originally Posted by ViralFrost
Okay, so if using slashes in the input, like for web addresses or directories, I would have to use raw_input(), right?


Yeah. BTW, in Python 3, there’s no raw_input() and input() works like Python 2’s raw_input().

You might also consider the following:

Code:
lead0 = (raw_input('Leading 0 (y/n): ').upper() == 'Y')
fmt = '{0}'
if lead0:
    fmt = '{0:02d}'
a = [fmt.format(x) for x in range(img_fnum + 1, img_lnum + 1)]


This would create the “a” list in one sweep. (It’s bad practice to change the list you’re going through in a “for” loop.)

Reply With Quote
  #13  
Old January 29th, 2012, 02:26 PM
ViralFrost ViralFrost is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 ViralFrost User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
I don't understand what you're doing with fmt ( '{0}' or '{0:02d}') please explain

Reply With Quote
  #14  
Old January 29th, 2012, 02:39 PM
ViralFrost ViralFrost is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 ViralFrost User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 17 sec
Reputation Power: 0
I tried changing
a = range(int(img_lnum)) to a = range(int(img_fnum), int(img_lnum))
but now the script gives an error of cannot concat 'str' and 'int ... I tried casting a[int(x)] in the for loop as a string, but still no luck... any ideas?

Reply With Quote
  #15  
Old January 29th, 2012, 03:28 PM
Duifmeneer Duifmeneer is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 7 Duifmeneer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 46 sec
Reputation Power: 0
Just a tip: If you write a script it might be handy if you put some comments (# 'text'). Gives better view of what is what and makes help easier.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > My first Python program!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap