python/Django下讀寫文件,等待,調用shell命令等技術總結

 

一、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)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章