Last active
August 29, 2015 14:02
-
-
Save hironow/6d02b96b594db24f8745 to your computer and use it in GitHub Desktop.
Using GMT from Python by subprocess and shlex packages.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from __future__ import (absolute_import, division, | |
print_function, unicode_literals) | |
from future_builtins import * | |
import shlex | |
import subprocess | |
import os | |
script = "gmt --version" | |
output_file = 'sample.txt' | |
class ScriptRunningError(Exception): | |
pass | |
def main(): | |
with open(output_file, 'w') as f: | |
proc = subprocess.Popen(shlex.split(script), env=os.environ, | |
stdout=f, stderr=subprocess.PIPE) | |
stdout, stderr = proc.communicate() | |
retcode = proc.returncode | |
del proc | |
if retcode == 1: | |
raise ScriptRunningError('This script could not run $ %s\n%s' % | |
(str(script), str(stderr))) | |
else: | |
return True | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5.1.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment