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.

5
  • Where is stderr going? Commented Dec 1, 2015 at 18:28
  • You getting a permissions error? Also what is cd /var/log/messagelog supposed to be doing? Commented Dec 1, 2015 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 Commented Dec 1, 2015 at 18:29
  • Do you have permissions to write in /var/log? Are you running your program as root? Commented Dec 1, 2015 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? Commented Dec 1, 2015 at 19:15

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

e.errno == errno.EEXIST
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, I added a link to an answer discussing it, how safe/unsafe it is depends on what the OP is doing.
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.

5 Comments

This is not a job for subprocess at all
The OP has stated Docs dont say much about storing the Output.. The sp.call really does not save the output. Popen offers you a way to do it.
yes but the os module is the correct way to check for or create a directory, not parsing the output of a subprocess
No argue about that. OP is abusing python to run a shell command.
Popen cannot be used for cd since it is a command hence why call was being used in the first place. Agree with Padriac

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.