Python

  1. Home
  2. Computing & Technology
  3. Python

Python Blog

From Al Lukaszewski, for About.com

Running Executables That Include Metacharacters

Friday November 21, 2008
When calling an external program from within Python, one is oft bound by the naming conventions used in developing the other program. It is bad form to use metacharacters in the base name of an executable, but this does not stop it from being done. If the program is not needed elsewhere, one can rename it, of course. But if there is any doubt, best to leave it as it is and work around it. You can do that by escaping the metacharacter in the same way one would handle a regular expression.

Having imported the os module, one can pass a system call with os.system(''). However, one will run into problems if a metacharacter is in the name of the executable. For example, let's say we saved the code from the recent Python and Tkinter guide to 'hello&tk.py'. If we wanted to call it from within Python's interpreter or another program, we need to escape the metacharacter. Consider:
>>> import os
>>> os.system('/path/to/program/hello\&tk.py')
0
>>>
If the executable is not in the path of your Python interpreter, you will need to use the absolute path, starting from the root or base of the directory tree. While the program is being executed, the Python interpreter will hang until the external program is run. On a Linux installation (or Unix-derivative), one can background the process using a trailing (and unescaped) ampersand (Windows users can try the subprocess module). If the process executes cleanly, Python returns 0.

Comments

January 8, 2009 at 9:01 am
(1) Alienkid says:

I am trying to make a python launcher for games on my computer(I am on windows xp)and when I use the escape character windows thinks it’s part of the directory structure, It also happens whit this on game when I type it like this code: system(’”C:\games\EA\C&C95\C&C95.exe”‘) or if type it like that with a \ before the “&”s, help.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore Python

About.com Special Features

Python

  1. Home
  2. Computing & Technology
  3. Python

©2009 About.com, a part of The New York Times Company.

All rights reserved.