Tom Tom - 2 months ago 7
Python Question

sys.path.append not honored for os.system command?

I have a very simple python script that used os.system to run a GDAL command. It is -> list target files -> loop them -> try os.system gdal_translate command. In the past I simply had a line towards the top of the script that was


My os.system command was

os.system("gdal_translate -of GTiff "+fullPFD+" "+fullNewName+" "+"-co COMPRESS=NONE --config GDAL_PDF_DPI "+res)

is the full path to the target PDF,
is the same thing with '.tif' substituted for '.pdf' and
is the resolution I want.

This worked in the past. Now when I run the script, no command prompt is launched. The code doesn't break and the print commands located around the os.system are used. The code is simply not running the os.system gdal command.

  • Please avoid discussing direct GDAL modules in Python IDE. I have been down that path unsuccessfully. I need a quick answer, which was

  • Other things to note are that since I ran this script last year, I have installed 2 more python versions, (Although I don't think that is the culprit.)

jez jez

Generally, sys refers to the Python system, not the operating system as a whole. Since you're attempting to invoke something outside of Python, it comes under the purview of os, not sys.

Specifically, sys.path is for importing Python modules. But you are not invoking Python code: you're invoking the system shell, i.e. the interpreter outside of Python to which you are escaping using os.system. On Windows, this is cmd.exe. The system shell does not use sys.path; instead, its path is specified in the environment variable PATH.

Environment variables are accessible from Python—in this case, you can query and change its value as os.environ['PATH']. Its value is a string, not a list, so you cannot .append(). Instead you might say something like:

bin_directory = r"C:\OSGeo4w\bin"
os.environ['PATH'] += os.path.pathsep + bin_directory

On Windows, os.path.pathsep is a semicolon. On POSIX-like systems it would be a colon. On both types of OS, the relevant variable is accessible as 'PATH' (on Windows it's really 'Path', but happily os.environ provides a case-invariant interface).

Side note: os.system is quick, dirty, and on its way to being deprecated. For alternatives, see Calling an external command in Python