1

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

sys.path.append(r"C:\OSGeo4w\bin")

My os.system command was

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

Where fullPDF is the full path to the target PDF, fullNewName is the same thing with '.tif' substituted for '.pdf' and res 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 os.system.
  • 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.)
das-g
  • 8,581
  • 3
  • 33
  • 72
Tom
  • 113
  • 4

1 Answers1

8

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

Community
  • 1
  • 1
jez
  • 12,803
  • 2
  • 30
  • 54
  • Thanks jez. And ya os.system is the quick and dirty. Will try to avoid it in the future. – Tom Jan 14 '16 at 20:12