0

I've tried methods that I have seen without luck and the Docs dont say much about storing the Output. I've tried the below:

import subprocess
f = open(r'/var/log/temp.txt','w')
subprocess.call('cd /var/log/messagelog', shell=True, stdout=f)
f.close()
with open("/var/log/temp.txt",'r') as f:
    for line in f:
            print line

All I'm trying to do is get the program to recognize that the directory does not exist. ive substituted a simple print statement for the time being. i CANNOT use check_output for this as updating Python on 30 different systems is highly impractical for this. print line should produce: "cd: can't cd to /var/log/messagelog". Hoping this is a rookie mistake I am making here so another set of eyes here would be greatly appreciated.

EDIT: All I'm trying to do here is see if the directory exists and if not, then create the directory. There may be other ways of doing this but everything I've seen points to subprocess. cd is not a program hence why i have to use call instead of Popen.

bladexeon
  • 606
  • 2
  • 8
  • 28
  • Where is stderr going? – PTBNL Dec 01 '15 at 18:28
  • You getting a permissions error? Also what is `cd /var/log/messagelog` supposed to be doing? – Padraic Cunningham Dec 01 '15 at 18:28
  • The directory does not exist and i get the "cd: can't cd to /var/log/messagelog" statement when the command is run so no permission denied error here. its supposed to try to cd to that directory, and if it get the "cant cd" error it should make that directory. otherwise it will pass – bladexeon Dec 01 '15 at 18:29
  • Do you have permissions to write in /var/log? Are you running your program as root? – salomonvh Dec 01 '15 at 18:47
  • If you need `subprocess.check_output()` on Python 2.6 or earlier, see [What's a good equivalent to python's subprocess.check_call that returns the contents of stdout?](http://stackoverflow.com/q/2924310/4279) – jfs Dec 01 '15 at 19:15

2 Answers2

4

use the os module to check if the directory exists and to create it if it does not potential race condition:

import os
if not os.path.isdir('/var/log/messagelog'):
    os.mkdir('/var/log/messagelog')

You can also just try to create it and check for specific error:

import os
try:
    os.mkdir(path)
except OSError as e:
    if e.errno == 17:
        print("dir exists")
    else:
         raise 

You can use the EEXIST constant if it makes it more readable:

import errno
import os

try:
    os.mkdir(path)
except OSError as e:
    if errno.EEXIST == e.errno:
        print("dir exists")
    else:
        raise 

This will only catch a directory exists error, any other OSError will be raised which may or may not be what you want.

Community
  • 1
  • 1
Padraic Cunningham
  • 160,756
  • 20
  • 201
  • 286
  • 2
    `e.errno == errno.EEXIST` – Andrea Corbellini Dec 01 '15 at 18:48
  • 2
    The first suggestion in this answer should probably just be removed since it creates a race condition. Or at the very least, it should mention that it creates a race condition. Otherwise, it is tempting to use because the code looks simpler (even though in reality it is far less safe). – eestrada Dec 01 '15 at 18:55
  • 1
    @eestrada, I added a link to an answer discussing it, how safe/unsafe it is depends on what the OP is doing. – Padraic Cunningham Dec 01 '15 at 19:01
0

You should use

proc = sp.Popen(..., stdout=filehandle)
err = proc.communicator()

Instead of sp.call. The output will be in the out.

Check the docs.

oz123
  • 23,317
  • 25
  • 106
  • 169