0

I've got a command that looks like this

subprocess.Popen('shp2pgsql -s 17932 \\storage1\dev1\gis\a.shp asmithe.myTable | psql -U asmithe -h example.org -d inventory -q', shell=True).wait()

heinous I know. It outputs to the screen and I don't want that. How can I supress it? The psql has the -q option for quiet but can't find anything simillar for shp2pgsql

Celeritas
  • 14,489
  • 36
  • 113
  • 194
  • Do you mean [this](http://stackoverflow.com/questions/4675728/redirect-stdout-to-a-file-in-python)? Or possibly simply assigning that call to a temporary variable? – TigerhawkT3 Jul 23 '15 at 21:19
  • related: http://stackoverflow.com/questions/16771117/why-should-we-use-stdout-pipe-in-subprocess-popen – NightShadeQueen Jul 23 '15 at 21:20
  • unrelated: 1. use raw string literals otherwise `'\a'` is a single character and your command shouldn't work at all. 2. use `check_call()` instead of `Popen().wait()` – jfs Jul 23 '15 at 22:01

1 Answers1

0

The easiest way is to capture stdout and stderr and send them to DEVNULL

(I'm using espeak 4; echo 'bye' as a test command; echo prints to stdout and espeak, well, speaks so will have output that isn't captured)

In [14]: p = subprocess.Popen("espeak 4; echo 'bye'", shell=True, \
             stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).wait()

In [15]: p = subprocess.Popen("espeak 4; echo 'bye'", shell=True).wait()
bye

The other thing you can do is send everything to /dev/null, if you're on a linux/unix machine

In [16]: p = subprocess.Popen("echo 'bye' > /dev/null 2>&1", shell=True).wait() 
         # note: this is bash. Works for me on zsh as well,
         # but might not work for other variants of sh
         # use command 2> /dev/null if you only want to redirect stderr.

Addendum: Wait, you're using a pipe, the first command is clearly outputting to stderr

For your script, if I were to do with linux commands alone, I'd write it this way:

subprocess.Popen('shp2pgsql -s 17932 \\storage1\dev1\gis\a.shp asmithe.myTable 2> /dev/null | '
    'psql -U asmithe -h example.org -d inventory -q', shell=True).wait()
     #implicit string concat is awesome for breaking up long lines
NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37