Reputation: 1001
I write a function which sets system time in linux. I write next code:
import time
import subprocess
def SetSystemTime(val):
try:
val = float(val)
except ValueError:
return
command = 'date -s"' + time.ctime(val) + '"'
subprocess.call(command)
On calling of that i get:
File "crc.py", line 96, in SetSystemTime(0) File "crc.py", line 12, in SetSystemTime subprocess.call(command) File "/usr/lib/python2.7/subprocess.py", line 493, in call return Popen(*popenargs, **kwargs).wait() File "/usr/lib/python2.7/subprocess.py", line 679, in init errread, errwrite) File "/usr/lib/python2.7/subprocess.py", line 1239, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory
Upvotes: 0
Views: 1099
Reputation: 76652
subprocess.call() normally takes a list of strings as its first argument. If you hand it a string X (as you do), that one will be converted to list with that whole string as its first argument. The first element of that list is executed with the rest of that list as arguments. So the underlying OS tries to execute the executable 'date -s"XXYYXXZ"' and cannot find it.
This is different from os.system() that hands the parameter to a shell that most often splits the string it gets at the spaces and then executes the first element split off.
Try:
command = ['date', '-s"' + time.ctime(val) + '"']
subprocess.call(command)
as the last two lines.
Upvotes: 1