3

Trying the below I get a file Import_Conf_Output.txt2 created as empty, can anyone tell me what is wrong?

cmd1 = 'my command'
os.system(cmd1 + "1>&/home/s_admin/Import_Conf_Output.txt" + "2>&/home/s_admin/Error.txt")
nick_gabpe
  • 5,113
  • 5
  • 29
  • 38
Rashmi G
  • 103
  • 2
  • 6
  • 1
    sure your command is outputting something? Would guess a space at the beginning of the `" 1>&..."` – Roelant Mar 15 '17 at 11:50
  • yes it is creating a file Import_Conf_Output.txt2, however when executed directly from cmd line : my cmd 1>&/home/s_admin/Import_Conf_Output.txt it works fine – Rashmi G Mar 15 '17 at 11:54
  • probably the lack of the space at the beginning of the string then :) – Roelant Mar 15 '17 at 12:05

2 Answers2

2

As noted in a comment and another answer, your command string is missing spaces. Thus the command you are sending to the shell is:

my command1>&/home/s_admin/Import_Conf_Output.txt2>&/home/s_admin/Error.txt

That said, the subprocess module is meant to replace os.system (and other similar devices). From the docs, the replacement for your command would be:

subprocess.call('my command' + ' 1> /home/s_admin/Import_Conf_Output.txt 2> /home/s_admin/Error.txt', shell=True)

You may want to look through the subprocess module's documentation to see if there is a better way to accomplish what you are doing.

Edit: To work given the command line as provided, the ampersands also should be removed from the redirection, as the different output streams are not going to be combined.

However, as gbtimmon noted in a comment, you can specify stdout and stderr in subprocess.call. The following line, using the ls command in place of my command, sent the output to ls.out. Since there was no error, ls.err was empty.

subprocess.call('ls', stdout=open('ls.out', 'w'), stderr=open('ls.err', 'w'))
Community
  • 1
  • 1
GreenMatt
  • 18,244
  • 7
  • 53
  • 79
  • 1
    He should really go with `subprocess.call([command], stdout=open(file1,"w"), stderr=open(file2,"w"))` – gbtimmon Mar 15 '17 at 12:42
  • tried with spaces and also subprocess, now the text file includes message '/bin/sh: /home/s_admin/Error.txt: ambiguous redirect' – Rashmi G Mar 15 '17 at 12:59
  • @RashmiG: Take out the ampersands ("&"). Those imply that you want to redirect multiple streams to one file, but that's not what you are doing. – GreenMatt Mar 15 '17 at 14:04
  • @gbtimmon: Thanks the suggestion. I didn't remember how to do that, nor did I have time to research when I was originally typing the answer. I've edited to add that. – GreenMatt Mar 15 '17 at 14:13
  • @RashmiG: Please see the updated answer. As gbtimmon said, the way to go is by directly specifying stdout & stderr arguments for [`subprocess.call()`](https://docs.python.org/2/library/subprocess.html#subprocess.call). – GreenMatt Mar 15 '17 at 14:16
  • Thanks for the inputs. on trying your command I get Permission denied: 'ls.out', shd i run this as root? – Rashmi G Mar 15 '17 at 15:09
  • @RashmiG: This is getting further and further away from the original question. Also, without know more specifics about what you're attempting, it is impossible for me to directly answer that (and even with specifics, I may not be able to provide a good answer). That said, it does seem you don't have permission to create that file in that directory. However, generally speaking, it's best to avoid running something as root, unless you have no choice. Could you direct your output to a file in a directory in which you have write permission? – GreenMatt Mar 15 '17 at 15:22
  • I think its getting tricky...however the output is redirecting to the files using the subprocess (subprocess.call(cmd1 + ' 1>&/home/s_admin/Import_Conf_Output.txt', shell=True))command, which answers my original question. Thanks all for your support. however I have some permission issues to deal with, will browse other options. – Rashmi G Mar 15 '17 at 15:59
0

your should call as:

cmd1 = 'my command'
os.system(cmd1 + " 1>/home/s_admin/Import_Conf_Output.txt" + " 2>/home/s_admin/Error.txt")
SHAHS
  • 462
  • 1
  • 5
  • 13
  • tried this no luck, now the text file includes message '/bin/sh: /home/s_admin/Error.txt: ambiguous redirect' – Rashmi G Mar 15 '17 at 13:03
  • ok then I would suggest use subprocess.Popen() and instead of using stdout=subprocess.pipe, create your files first and use its file descriptor here e.g sdout=error_files. it should work. – SHAHS Mar 15 '17 at 13:11