Find Yourself With Python's os Module
Tuesday March 25, 2008
Last week, I looked at one of the several ways to issue system calls and execute other programs through Python; there are, to be sure, several others (e.g., popen, popen2, popen3, popen4, as well as the various offshoots of spawnv). In that example, 'kcalc' was already in the system path, the series of default directories where the operating system looks for executable files. But in order to issue a local command (i.e., run a program from within a set directory), you must either use the absolute path for the program file or ensure that you are in the same directory as the executable.
To verify your location, use
To verify your location, use
os.getcwd() to return your current working directory (hence 'cwd'). You can then verify your location accordingly. Note that this only works if you know the precise location of the executable. In the next post, we will look at how to search a directory listing for a given file name. For more on how to use the Python library, see the guides on the various modules.