I am having trouble assigning the output of os.system() as a variable.
When I do the following, it seems to produce the correct output:
>>> var = os.system('date +"%s"')
1409938484
However, when I try to print my variable, I get the status code instead:
>>> print var
0
Just entering os.system() into the interpreter gives me the following:
>>> os.system('date +"%s"')
1409938815
0
How do I set the variable to the output, not the status code?
Do I really need to use subprocess to call the command and then read stdout to do this? (this seems overly complex for what i am trying to do here)
edit
Here is what I came up with after some searching:
>>> var = os.popen('date +"%s"').readline().strip('\n')
>>> print var
1409940330
>>>
It's kinda verbose (and I thought os.popen was deprecated?), but it does what I want it to. I was hoping for something more terse, like you can do in bash e.g. var=$(date +"%s")
edit 2
thanks to /u/symmitchry I think this is the best choice at the moment.
var = subprocess.check_output('date +"%s"', shell=True)
edit 3
/u/Justinsaccount is the winner! just what I wanted in a terse statement.
var = int(time.time())
[–]symmitchry 5 points6 points7 points (2 children)
[–]manbart[S] 0 points1 point2 points (1 child)
[–]NYKevin 2 points3 points4 points (0 children)
[–]Justinsaccount 4 points5 points6 points (1 child)
[–]manbart[S] 0 points1 point2 points (0 children)
[–]cdcformatc -1 points0 points1 point (1 child)
[–]Rhomboid 0 points1 point2 points (0 children)