在命令行敲一串长的命令,枯燥麻烦。
#coding:utf-8
import sys
import os
import subprocess
if len(sys.argv) == 2:
#节省输入,不输入后缀
#直接使用参数的第2个值
file = sys.argv[1] + '.ui'
#检查输入文件是否存在
if not os.path.exists(file):
print('input file is not exited.')
exit(1)
#分离文件名和扩展名
#本转换,和UI文件在同一目录
f, ext = os.path.splitext(file)
dist = f + '.py'
#执行的命令
cmd = 'pyuic4 ' + file + ' -o ui/' + dist
#使用subprocess模块,os.command也可以用
code = subprocess.call(cmd, shell=True)
#输出结果
if code == 0:
print('%s --> %s success.' % (file, dist))
else:
print('%s --> %s failure.' % (file, dist))
使用Python编写一个程序,随便练练语法。
原文地址:http://blog.csdn.net/fengyu09/article/details/37592241