python文件读写操作与linux shell变量命令交互执行的方法
作者:shichen2014 时间:2022-10-24 06:37:55
本文实例讲述了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程序设计有所帮助。
标签:python,文件,linux,shell
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
初探TensorFLow从文件读取图片的四种方式
2021-08-06 06:04:34
原生js实现移动小球(碰撞检测)
2024-04-19 09:56:01
![](https://img.aspxhome.com/file/2023/5/136005_0s.jpg)
Python 面向切面编程 AOP 及装饰器
2021-05-07 14:16:36
![](https://img.aspxhome.com/file/2023/2/75212_0s.webp)
sqlserver中with(nolock)深入分析
2024-01-27 13:39:48
浅谈mysql的索引设计原则以及常见索引的区别
2024-01-22 23:58:49
使用FSO把文本信息导入数据库
2007-09-27 20:20:00
vue开发中关于axios的封装过程
2024-04-30 10:40:24
TypeScript类型检查详谈及火爆原因
2022-01-24 11:48:41
![](https://img.aspxhome.com/file/2023/1/131811_0s.png)
Python光学仿真学习衍射算法初步理解
2021-01-07 13:54:46
![](https://img.aspxhome.com/file/2023/3/110373_0s.png)
Mysql锁内部实现机制之C源码解析
2024-01-12 21:13:25
![](https://img.aspxhome.com/file/2023/4/70454_0s.png)
python中if的基础用法(if else和if not)
2021-02-01 01:56:18
![](https://img.aspxhome.com/file/2023/0/128610_0s.png)
MySql多表链接查询详细教程
2024-01-16 06:03:33
![](https://img.aspxhome.com/file/2023/2/68582_0s.png)
Django实现WebSocket在线聊天室功能(channels库)
2023-04-01 00:41:55
![](https://img.aspxhome.com/file/2023/1/114591_0s.png)
MySQL中使用replace、regexp进行正则表达式替换的用法分析
2024-01-15 13:34:02
mysql启用skip-name-resolve模式时出现Warning的处理办法
2024-01-25 09:38:44
优化 SQL Server 索引的小技巧
2012-10-07 11:00:07
django创建自定义模板处理器的实例详解
2022-07-29 19:50:00
django 解决manage.py migrate无效的问题
2021-08-18 02:28:51
pandas DataFrame 根据多列的值做判断,生成新的列值实例
2023-11-11 01:48:11
![](https://img.aspxhome.com/file/2023/5/71575_0s.jpg)
flask上传作品之dbm操作的实现
2022-06-29 15:25:41
![](https://img.aspxhome.com/file/2023/1/94071_0s.png)