2

I am trying to do something like:

f = subprocess.check_output("./script.sh ls -l test1/test2/test.log", shell=True)

when I print f, I get value 0. I tried using subprocess and then read() and even then i dont get the details of the file. I need to verify the size of the file..

Not sure how it can be done.

Any help?

When I used

f = os.system("./script.sh ls -l test1/test2/test.log"), I get the output but does not get saved in f. Something like stdoutput or something..

UPDATED: I used

f = os.popen("./script.sh ls -l test1/test2/test.log 2>&1")

if I ran the same command in quotes above, directly on CLI myself, it works fine but if I used the above in a script OR used s = f.readline(), the script stops, I need to hit "return" before the script can proceed..

Why is that? I need 's' because I need to process it.

user2921139
  • 1,211
  • 4
  • 14
  • 24
  • Please show actual code. That `os.system` line is obviously not what you ran, because it's a SyntaxError, and even if it weren't, you'd almost certainly get some kind of error from `script.sh` because it can't find a file named `test.log,`. And "I tried using subprocess and then read()" means you almost certainly missed something that we could explain to you if we saw what you tried, but can't explain without seeing anything. – abarnert Oct 24 '14 at 21:45
  • What is stpping you using the methods in the OS module to check this size of the file ? – Tony Suffolk 66 Oct 24 '14 at 21:46
  • os.system is more or less deprecated, you are supposed to use the more powerful [subprocess module](https://docs.python.org/2/library/subprocess.html) instead. – Bas Swinckels Oct 24 '14 at 21:47
  • @inspectorG4dget: That question is about getting the output of a function call that's been defined or imported in the current script/module, not about getting the output of an external program. I'm sure this is a dup of many questions, but I don't think that's the right one. – abarnert Oct 24 '14 at 21:48
  • @abarnert: as worded, that question might not be exactly the same. But the solution does transcend that question into this one. Hence why I voted for closure – inspectorG4dget Oct 24 '14 at 21:51
  • [This question](http://stackoverflow.com/questions/19661859/getting-output-from-os-system-no-subprocess), even though it explicitly isn't a dup, has a link to a few dups, which then have links to a bunch of others. – abarnert Oct 24 '14 at 21:51
  • Once a duplicate has been identified, it is best to ask a new question if you discover that your code is in fact different from what you posted the first time or have additional issues. Editing the old question into something else just makes a mess for future visitors. – Paul Oct 24 '14 at 23:07

1 Answers1

1

You can use subprocess.check_output:

f = subprocess.check_output("./script.sh ls -l test1/test2/test.log",shell=True)
print(f)

You can split into a list of individual args without using shell=True:

f = subprocess.check_output(['./script.sh', 'ls', '-l', 'test1/test2/test.log']))
Gabriel
  • 32,750
  • 58
  • 187
  • 337
Padraic Cunningham
  • 160,756
  • 20
  • 201
  • 286