Hỏi đáp
Chia sẻ kiến thức, cùng nhau phát triển
Xuất stdout từ 1 command được send từ python tới linux
Mọi người ơi, cho e hỏi với :
Hiện tại, em đang có 1 case định làm tự động hóa cho linux như sau :
1. send command : './bhtest -Dm .desc2 > 2.log' từ script python tới linux server
2. giá trị trả về khi run command này sẽ được đưa vào file text result.txt
3. Sau đó tiếp tục run tiếp command thứ 2 : './bhtest -Dm .desc > 1.log'
4. lưu giá trị trả về vào dòng 2 của result.txt, nếu trong trường hợp là giá trị trả về là rỗng hoặc có đến 2 giá trị trả về thì e nên làm thế nào ạ ?
Code :
import subprocess
import sys
# Send command to linux
return_value = subprocess.check_output('./bhtest -Dm .desc2 > 2.log',stderr=subprocess.STDOUT,shell=True)
# Export value return to result.txt
original_stdout = sys.stdout
f = open('result.txt','r')
sys.stdout = f
print(return_value)
sys.stdout = original_stdout
f.close()
Error :
Traceback (most recent call last):
File "compare1value.py", line 21, in <module>
print(return_value)
IOError: [Errno 9] Bad file descriptor