Home  >  Article  >  Backend Development  >  python文件读写操作与linux shell变量命令交互执行的方法

python文件读写操作与linux shell变量命令交互执行的方法

WBOY
WBOYOriginal
2016-06-06 11:20:501794browse

本文实例讲述了python文件读写操作与linux shell变量命令交互执行的方法。分享给大家供大家参考。具体如下:

python对文件的读写还是挺方便的,与linux shell的交互变量需要转换一下才能用,这比较头疼。

代码如下:

代码如下:


#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

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn