您的位置:首页 > 运维架构 > Shell

python/Django下读写文件,等待,调用shell命令等技术总结

2011-11-22 20:19 1226 查看
一、python/Django 写文件

file_object = open('/opt/ result.txt','w')

file_object.write(gg)

file_object.close()

打开/opt目录下的result.txt文件,并将字符串gg写入result.txt文件

二、python/Django 读文件

file_object = open('/opt/ test.py')

try:

gg = file_object.read()

finally:

file_object.close()

打开/opt目录下的test.py文件,将文件内容赋值给gg,作为一个长字符串

三、python/Django 程序中等待

import time

time.sleep(5)

四、python/Django 调用shell命令

import os, sys

cmdd = "python /opt/test.py"

os.system(cmdd)

或者

import os, sys

cmdd = "python /opt/test.py "

gg = os.popen(cmdd).read()

或者

import subprocess

process = subprocess.Popen(['service', 'httpd', 'restart'])

或者

import subprocess

r = subprocess.call("service httpd restart 1>$HOME/out 2>$HOME/error",Shell=True)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: