0

The following is the code:

import os
#f=os.popen('date -d @1358193598 +%m/%d/%y')
f=os.popen('date')
print(f)

I tied both ways, and it gave me an error of "broken pipe". Do you happen to know how to deal with this situation? I also tried to use the subprocess module, and it does not work either. The error message is as the following:

Traceback (most recent call last):
  File "t_2.py", line 23, in <module>
    dates.append(transfer_date_format(raw_date))
  File "t_2.py", line 6, in transfer_date_format
    stdin=subprocess.PIPE)
  File "/usr/lib64/python2.6/subprocess.py", line 639, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.6/subprocess.py", line 1228, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
  • 1
    Because you need full path or os.system (it uses PATH env). But why do you need it? There is an API in Python for dates – user996142 Jun 27 '17 at 20:26
  • 3
    use datetime module – RomanPerekhrest Jun 27 '17 at 20:26
  • 2
    good point. maybe I can simply use python module to implement this instead of running unix commands. I will dig into it. – josephS Jun 27 '17 at 20:28
1

You can try with os.system

f=os.system('date')

Here it will save exit code to the variable f

If you want to take output of executed command to the variable, try

f=os.popen('date').read()
print f
  • "f=os.system" will only run the command, right? what if I'd like to get the output of the command to a string instead? – josephS Jun 27 '17 at 20:26
  • You can take output of executed command using f=os.popen('date').read() – Sourav Jun 27 '17 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.