Найти - Пользователи
Полная версия: Различие os.system и call()
Начало » Python для новичков » Различие os.system и call()
1
Fre
В чем различие? что лучше использовать?
и немного кода:
if not(call(test, shell=True)):
result='OK'
else:
result=test
print(test_name + ‘ … ’ + result)

почему для грамотной работы приходится ставить not?
поясню, при удачном выполнении команды uname | grep Linux
возвращается непустая строка ‘Linux’
при неудачном выполнении - возвращается пустая строка, почему так?

и еще проблема, если call или os.system стоит в условии if, он тем не менее выводит на экран результат, как избежать этого?
asilyator
А где ты этот call() взял?
Подозреваю, что он возвращает код возврата программы.
py.user.next
>>> import subprocess
>>> print(subprocess.getstatusoutput.__doc__)
Return (status, output) of executing cmd in a shell.
    Execute the string 'cmd' in a shell with os.popen() and return a 2-tuple
    (status, output).  cmd is actually run as '{ cmd ; } 2>&1', so that the
    returned output will contain output or error messages.  A trailing newline
    is stripped from the output.  The exit status for the command can be
    interpreted according to the rules for the C function wait().  Example:
    >>> import subprocess
    >>> subprocess.getstatusoutput('ls /bin/ls')
    (0, '/bin/ls')
    >>> subprocess.getstatusoutput('cat /bin/junk')
    (256, 'cat: /bin/junk: No such file or directory')
    >>> subprocess.getstatusoutput('/bin/junk')
    (256, 'sh: /bin/junk: not found')
    
>>> 
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB