This example describes the way Python file read and write operations interact with Linux shell variable commands. Share to everyone for your reference. Specifically as follows:
Python is easy to read and write files, and the Linux shell interaction variables need to be converted to use, which is more headaches.
The code is as follows:
Copy Code code as follows:
#coding =utf-8
#!/usr/bin/python
Import OS
Import time
#python执行linux命令
Os.system (': >./aa.py ')
#人机交互输入
S = Raw_input ("Input:")
os.environ[' s ']=str (s)
#把字符串S写入文件
Output = open ('./aa.py ', ' a ')
Output.write (S)
#关闭文件
Output.close ()
#python中的思考时间
Time.sleep (1)
#获取文件内容
f = open ('./aa.py ', ' R ')
Read = F.read ()
#变量间的转换
os.environ[' read ']=str (read)
Os.system (' $read ')
F.close ()
Var=os.popen (' Ifconfig '). Read ()
Print Var
I hope this article will help you with your Python programming.