python вывод команд в переменную
Модератор: Модераторы разделов
-
v8jupiter
- Сообщения: 53
- ОС: GNU/LInux
python вывод команд в переменную
Здраствуйте. Только начал осваивать python. Не могу никак понять как сделать что бы например вывод команды df записать в перменную , а потом оттуда вытянуть с помощью регулярок нужные значения.
-
hippi90
- Сообщения: 83
- ОС: Debian testing
Re: python вывод команд в переменную
Смотрите в документации subprocess.Popen.
-
v8jupiter
- Сообщения: 53
- ОС: GNU/LInux
Re: python вывод команд в переменную
Спасибо. Вот так сделал:
df = subprocess.Popen("df -h /dev/sda5", shell = True, stdout = subprocess.PIPE)
# stdout = subprocess.PIPE - означает, что вывод программы перехватывается
df1 = df.communicate()[0] #получаем перехваченный вывод
df = subprocess.Popen("df -h /dev/sda5", shell = True, stdout = subprocess.PIPE)
# stdout = subprocess.PIPE - означает, что вывод программы перехватывается
df1 = df.communicate()[0] #получаем перехваченный вывод
-
Rammix
- Сообщения: 3
- ОС: OpenSUSE
Re: python вывод команд в переменную
Спасибо что не только нашёл, но и поделился. 
Ато ман по subprocess какой-то невнятный, слёту не всё ясно.
Ато ман по subprocess какой-то невнятный, слёту не всё ясно.
-
Stauffenberg
- Сообщения: 2041
- Статус: ☮ PEACE ☮
- ОС: открытая и свободная
Re: python вывод команд в переменную
Код: Выделить всё
#!/usr/bin/env python
import os
f = os.popen("date").read()
print fКод: Выделить всё
> python --version
Python 2.7.2
> ./ufo.py
Fri Oct 5 19:37:13 CEST 2012Labor omnia vincit
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” (Brian Kernighan)
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” (Brian Kernighan)
Спасибо сказали:
-
Encore
- Сообщения: 656
- Статус: loading..............
- ОС: Debian
Re: python вывод команд в переменную
os.popen(command[, mode[, bufsize]])
Deprecated since version 2.6: This function is obsolete. Use the subprocess module.
Deprecated since version 2.6: This function is obsolete. Use the subprocess module.
-
Stauffenberg
- Сообщения: 2041
- Статус: ☮ PEACE ☮
- ОС: открытая и свободная
Re: python вывод команд в переменную
Да, я в курсе. Для этого я сначала вывод версии интерпретатора и показал
P.S.
Проблема у этого метода действительно есть. Cвязана она с тем, что при выполнении открывается новый shell. Это не всегда желательно.
Labor omnia vincit
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” (Brian Kernighan)
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” (Brian Kernighan)