简单了解python调用其他脚本方法实例
1.用python调用python脚本
#!/usr/local/bin/python3.7 import time import os count = 0 str = ('python b.py') result1 = os.system(str) print(result1) while True: count = count + 1 if count == 8: print('this count is:',count) break else: time.sleep(1) print('this count is:',count) print('Good Bye')
另外一个python脚本b.py如下:
#!/usr/local/bin/python3.7
print('hello world')
运行结果:
[python@master2 while]$ python a.py
hello world
this count is: 1
this count is: 2
this count is: 3
this count is: 4
this count is: 5
this count is: 6
this count is: 7
this count is: 8
Good Bye
2.python调用shell方法os.system()
#!/usr/local/bin/python3.7 import time import os count = 0 n = os.system('sh b.sh') while True: count = count + 1 if count == 8: print('this count is:',count) break else: time.sleep(1) print('this count is:',count) print('Good Bye')
shell脚本如下:
#!/bin/sh
echo "hello world"
运行结果:
[python@master2 while]$ python a.py
hello world
this count is: 1
this count is: 2
this count is: 3
this count is: 4
this count is: 5
this count is: 6
this count is: 7
this count is: 8
Good Bye
3.python调用shell方法os.popen()
#!/usr/local/bin/python3.7 import time import os count = 0 n = os.system('sh b.sh') while True: count = count + 1 if count == 8: print('this count is:',count) break else: time.sleep(1) print('this count is:',count) print('Good Bye')
运行结果:
[python@master2 while]$ python a.py
<os._wrap_close object at 0x7f7f89377940>
['hello world\n']
this count is: 1
this count is: 2
this count is: 3
this count is: 4
this count is: 5
this count is: 6
this count is: 7
this count is: 8
Good Bye
os.system.popen() 这个方法会打开一个管道,返回结果是一个连接管道的文件对象,该文件对象的操作方法同open(),可以从该文件对象中读取返回结果。如果执行成功,不会返回状态码,如果执行失败,则会将错误信息输出到stdout,并返回一个空字符串。这里官方也表示subprocess模块已经实现了更为强大的subprocess.Popen()方法。
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
- scrapy一些非常实用资料整理,与其他mongdb、django、redis、solr、tor结合使用以及用Python脚本调用等等方法
- Nodejs中调用系统命令、Shell脚本和Python脚本的方法和实例
- Nodejs中调用系统命令、Shell脚本和Python脚本的方法和实例
- Nodejs中调用系统命令、Shell脚本和Python脚本的方法和实例
- 调用其他python脚本文件里面的类和方法过程解析
- Nodejs中调用系统命令、Shell脚本和Python脚本的方法和实例
- python实现调用其他python脚本的方法
- python实现调用其他python脚本的方法
- PHP魔术方法的简单调用实例
- C#调用脚本语言(三)-- IronJS 与 IronLua 简单方法性能比较
- python 类方法__call__拦截调用实例介绍
- python调用shell脚本的方法
- python文件中的__name__=='__main__'的使用及调用其他py文件中的函数方法
- Js调用Java方法并互相传参的简单实例
- Android获取其他包的Context实例,然后调用它的方法
- JAVA RMI远程方法调用简单实例
- c++中调用python脚本提示 error LNK2001: 无法解析的外部符号 __imp_Py_Initialize等错误的解决方法
- python的subprocess:子程序调用(调用执行其他命令);获取子程序脚本当前路径问题
- Python脚本简单实现打开默认浏览器登录人人和打开QQ的方法
- Python中在脚本中引用其他文件函数的实现方法