Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

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 October 4th, 2004, 03:29 PM
sam_kh918's Avatar
sam_kh918 sam_kh918 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 323 sam_kh918 User rank is Corporal (100 - 500 Reputation Level)sam_kh918 User rank is Corporal (100 - 500 Reputation Level)sam_kh918 User rank is Corporal (100 - 500 Reputation Level)sam_kh918 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 11 h 53 m 49 sec
Reputation Power: 9
Launching applications using Python

Hello,

Is there anyway to launch the windows application from within a python script. I mean when I want to launch an application on my computer desktop, I double click on it. But is there anyway to do that by a python script?

Reply With Quote
  #2  
Old October 4th, 2004, 03:59 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,254 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 8 h 9 m
Reputation Power: 265
there are several ways to do it.

The simplest is to use os.system(command) to run an external command. This will launch an application - either the executable needs to be on the path, or you need to specify the full path.

e.g.

Code:
#run the windows calculator:
import os
os.system('calc.exe')


The system function will normally wait until the application has been closed before returning, which may or may not be what you want. If you want to launch an application and have the python script continue, then you can use the Windows start command:

Code:
#launch the windows calculator and continue
import os
os.system('start calc.exe')


If you want to run a command line program and return the output to python you will need to use one of the popen functions. That is a much bigger topic, so I will not cover it here.

Dave - The Developers' Coach

Reply With Quote
  #3  
Old October 5th, 2004, 11:14 AM
chuck54 chuck54 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Andorra / England
Posts: 80 chuck54 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 40 m 8 sec
Reputation Power: 5
Hi, im just trying to fathom out some things for a cdrecord gui. Can this command be used the same way with a linux shell? And would it take extra parameters including strings. ie "nano -s $myfile"
If there is some documentation, im sure i can find everything else i need. Sorry if my lingo isnt quite right, (is it a string in python?) but im sure you know what i mean.

Thanks.

Reply With Quote
  #4  
Old October 5th, 2004, 05:42 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 11 m 13 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
os.system() will work regardless of platform, just like in C/C++ if you're familiar with any of these. Of course the command you need to issue will probably be dependent on the platform the program is running on. Oh, and yes: it is called a string in Python.

check out the os module: http://www.python.org/doc/2.3.4/lib/module-os.html; you might also want to check out the sys module i.e. you can check the platform Python is running on using sys.platform.

Quote:
>>> import sys
>>> sys.platform
'darwin'
>>>


Mark.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : October 5th, 2004 at 05:47 PM.

Reply With Quote
  #5  
Old October 6th, 2004, 02:57 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
Passing a parameter to notepad using a string:
Code:
>>> import os
>>> fname = "readme.txt"
>>> os.system("notepad"+" " + fname)

You do the same with any command that takes command line parameters (on any platform). Note that you may get a console window (xterm) - if this is not what you want then checkout the process spawning commands like spawnv and popen (see python help files for details) A search of this forum will probably show some examples.

grim
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #6  
Old October 6th, 2004, 06:50 PM
p4j p4j is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 27 p4j User rank is Lance Corporal (50 - 100 Reputation Level)p4j User rank is Lance Corporal (50 - 100 Reputation Level)p4j User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 2 h 37 m 17 sec
Reputation Power: 0
Send a message via MSN to p4j
Another option besides os.system is os.startfile.

os.startfile is identical to clicking on a file (or application) in Windows Explorer or the desktop. By supplying the os.startfile function with the file name, it will open that file in the appropriate application. I'm not sure if it will work under Linux or not.

For further information, see the doc string for the startfile function.

Reply With Quote
  #7  
Old October 6th, 2004, 06:55 PM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 10
os.system! I knew there was a tidier way than os.spawnv() the last time I was trying to remember how to launch programs, but I couldn't remember it. Oh well.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Launching applications using Python


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT