[Tutor] starting a program from python

Kent Johnson kent37 at tds.net
Sun Jul 23 22:52:43 CEST 2006


johnsonv3 wrote:
> Hi,
> Using Windows XP Home & python 234
> I am trying to start python programs that run Panda 3D without having 
> to open command prompt and type a path every time I run a program.
>  
> This pandastart.bat file works...
>  
> cd C:\Panda3D-1.2.3\mystuff
> ppython bvd.py
>  
> and from python shell this works....
>  
> >>> subprocess.Popen("pandastart.bat")
> <subprocess.Popen object at 0x00E07DD0>
>  
> But it is not flexible to start other file names..
>  
> So I would like to write a python script similar to this....
>  
> import subprocess
> panda = raw_input("Enter name of Panda python program to start 
> (fileName.py)")
> subprocess.Popen("C:\panda3D-1.2.3\mystuff\panda")
Here panda is the name of a variable whose value is the name of the file 
you want to run. Python won't automatically insert the value of a 
variable into a string, it thinks you want to Popen a file called 
"C:\panda3D-1.2.3\mystuff\panda". Since there is no file with that name, 
you get an error.

You can use string formatting to get the name of the file into the 
string for Popen(). Try this:
subprocess.Popen("C:\panda3D-1.2.3\mystuff\%s" % panda)

For more info on string formatting:
http://docs.python.org/lib/typesseq-strings.html

Kent
>  
> But this is the result...
>  
> >>> subprocess.Popen("C:\panda3D-1.2.3\mystuff\bvd.py")
> Traceback (most recent call last):
>   File "<pyshell#19>", line 1, in ?
>     subprocess.Popen("C:\panda3D-1.2.3\mystuff\bvd.py")
>   File "C:\Python24\lib\subprocess.py", line 542, in __init__
>     errread, errwrite)
>   File "C:\Python24\lib\subprocess.py", line 706, in _execute_child
>     startupinfo)
> WindowsError: [Errno 123] The filename, directory name, or volume 
> label syntax is incorrect
>  
> Also tried the below (change to the path) with result as indicate 
> below....
>  
> >>> subprocess.Popen("C:\panda3D-1.2.3\mystuff bvd.py")
> Traceback (most recent call last):
>   File "<pyshell#20>", line 1, in ?
>     subprocess.Popen("C:\panda3D-1.2.3\mystuff bvd.py")
>   File "C:\Python24\lib\subprocess.py", line 542, in __init__
>     errread, errwrite)
>   File "C:\Python24\lib\subprocess.py", line 706, in _execute_child
>     startupinfo)
> WindowsError: [Errno 5] Access is denied
>  
> Have tried using os.system and os.startfile with similar results
>  
> Suggested solution?
> Thanks. 
>  
>  
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   




More information about the Tutor mailing list