Python 中os.system() 与os.exec*() 的区别
来源:互联网 发布:珍宝猫粮怎么样 知乎 编辑:程序博客网 时间:2024/05/20 03:41
在用高通的平台做Android开发时,Modem端的软件经常需要手动Push进去,并且文件有很多。用ADB命令手动Push进去很麻烦,所以写了一个脚本来干活。
Modem的文件都是以modem开头的
modem.b00, modem.b10, modem_fw.b11, xxx
#!/usr/bin/env python
########################################################
#
# Author: Winva
# Date: 2012-7-9
########################################################
'''
This script is used for pushing modem firmware to Phone.
PC dirctory fm/modemXXX, phone /system/etc/firmware
run $adb push XXX /system/etc/firmware
Current can run on Linux, for Window need change '/' to '\\'
'''
import sys
import re
import os
# find files and do push
help = '''Usage: AutoPush inputDirectoryPC matchString outputDirectoryPhone
matchString: string to match the file name
inputDirectoryPC: directory that includes the input files
outputDirectoryPhone: directory in phone that the files are pushed to
Example: AutoPush ~/fm/ modem /system/etc/firmware
'''
def main():
# check parameters
argn = len(sys.argv)
if argn == 1 :
print 'You must input the input directory!'
print help
sys.exit()
elif argn == 2 :
path = sys.argv[1]
matchString = 'modem'
phoneDirectory = '/system/etc/firmware'
elif argn == 3 :
path = sys.argv[1]
matchString = sys.argv[2]
phoneDirectory = '/system/etc/firmware'
else :
path = sys.argv[1]
matchString = sys.argv[2]
phoneDirectory = sys.argv[3]
if os.path.isdir(path) :
print path
else :
print 'Error, %s not exist!' % path
sys.exit()
# if path has a '/' for linux or '\\' for win?
# in case of missing a '/'
if path[-1] != '/' :
path = path + '/'
fileList = os.listdir(path)
foundFileNumber = 0
for fileName in fileList :
file = path + fileName
print file
# if the file is match?
m = re.match(matchString, fileName)
if m is not None :
if os.path.isfile(file) :
# Execute the operation
print 'Found a file: %s' % fileName
os.execlp('adb','adb', 'push',file, phoneDirectory )
foundFileNumber += 1
print '%d files handled!' % foundFileNumber
if __name__ == '__main__':
main()
运行程序:$ ./AutoPy.py firmware/
但是只有一个文件被Push进去了,看来是execlp()把Process抢走了,执行完了就退出了。
仔细查了一下Python的帮助
os.exec*()
The various exec*() functions take a list of arguments for the new program loaded into the process.
These functions all execute a new program, replacing the current process; they do not return. On Unix, the new executable is loaded into the current process, and will have the same process id as the caller. Errors will be reported as OSError exceptions.
The current process is replaced immediately.
当前的shell会被Adb程序替代,所以执行一次就退出。
还有另外一个函数
os.system(command)
Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations.
这个命令是在subshell里执行,执行完还会回到shell。
改成这样就可以了:
cmd = 'adb push ' + file + ' ' + phoneDirectory
os.system(cmd)
Modem的文件都是以modem开头的
modem.b00, modem.b10, modem_fw.b11, xxx
#!/usr/bin/env python
########################################################
#
# Author: Winva
# Date: 2012-7-9
########################################################
'''
This script is used for pushing modem firmware to Phone.
PC dirctory fm/modemXXX, phone /system/etc/firmware
run $adb push XXX /system/etc/firmware
Current can run on Linux, for Window need change '/' to '\\'
'''
import sys
import re
import os
# find files and do push
help = '''Usage: AutoPush inputDirectoryPC matchString outputDirectoryPhone
matchString: string to match the file name
inputDirectoryPC: directory that includes the input files
outputDirectoryPhone: directory in phone that the files are pushed to
Example: AutoPush ~/fm/ modem /system/etc/firmware
'''
def main():
# check parameters
argn = len(sys.argv)
if argn == 1 :
print 'You must input the input directory!'
print help
sys.exit()
elif argn == 2 :
path = sys.argv[1]
matchString = 'modem'
phoneDirectory = '/system/etc/firmware'
elif argn == 3 :
path = sys.argv[1]
matchString = sys.argv[2]
phoneDirectory = '/system/etc/firmware'
else :
path = sys.argv[1]
matchString = sys.argv[2]
phoneDirectory = sys.argv[3]
if os.path.isdir(path) :
print path
else :
print 'Error, %s not exist!' % path
sys.exit()
# if path has a '/' for linux or '\\' for win?
# in case of missing a '/'
if path[-1] != '/' :
path = path + '/'
fileList = os.listdir(path)
foundFileNumber = 0
for fileName in fileList :
file = path + fileName
print file
# if the file is match?
m = re.match(matchString, fileName)
if m is not None :
if os.path.isfile(file) :
# Execute the operation
print 'Found a file: %s' % fileName
os.execlp('adb','adb', 'push',file, phoneDirectory )
foundFileNumber += 1
print '%d files handled!' % foundFileNumber
if __name__ == '__main__':
main()
运行程序:$ ./AutoPy.py firmware/
但是只有一个文件被Push进去了,看来是execlp()把Process抢走了,执行完了就退出了。
仔细查了一下Python的帮助
os.exec*()
The various exec*() functions take a list of arguments for the new program loaded into the process.
These functions all execute a new program, replacing the current process; they do not return. On Unix, the new executable is loaded into the current process, and will have the same process id as the caller. Errors will be reported as OSError exceptions.
The current process is replaced immediately.
当前的shell会被Adb程序替代,所以执行一次就退出。
还有另外一个函数
os.system(command)
Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations.
这个命令是在subshell里执行,执行完还会回到shell。
改成这样就可以了:
cmd = 'adb push ' + file + ' ' + phoneDirectory
os.system(cmd)
- Python 中os.system() 与os.exec*() 的区别
- python os.popen与os.system区别
- python中os.popen, os.system()区别
- python中os.system、os.popen、subprocess.popen的区别
- python中os.popen, os.system()区别(转自:ccorz)
- Python中 os.system()与os.popen()
- python中那纠结的os.system()与空格处理
- python os.system os.popen 区别
- python os.system os.popen 区别
- python os.popen()和os.system()区别
- python中os.system()和os.popen()的返回值
- python中os.system()的返回值
- Python中os.system()的返回值
- python中os.system()的返回值
- python中os.system()的返回值
- python中os.system()的返回值
- python中os.system()的返回值
- python中os.system()的返回值
- ORACLE directory 目录
- TrackMouseEvent &
- iReport 4.1 报表制作,子报表,实例解析
- 代码高效性和健壮性的权衡
- c++正则表达式
- Python 中os.system() 与os.exec*() 的区别
- blog 用超链接来显示博文的列表,点击以后进入博文
- Postgresql允许远程访问
- 什么是PCM?它和.wav文件是什么关系?
- 数据说Digg的兴衰史 从辉煌到没落
- C++关于CFileDialog的更改路径问题及解决方法
- CreateProcess c
- perl的子程序sub返回值不改变的解决方法
- C# 线程的挂起与恢复