os.environ 系统变量,是一个dict,windows 下 key都为大写
sys 和解释器有关
sys.argv 是script参数
import os
os.getcwd()
os.chdir(dir)
os.listdir(dir)
import shutil
shutil.copyfile(file, new file name of dir)
shutil.move(file, new file name of dir)
import glob
glob.glob('*.py') #类似于 unix 的 ls
import sys
print sys.argv
在 python shell 中执行一个 script
execfile(filename) In python 3.0 exec(open(filename).read())在 DOS/Shell 环境下执行 python script
python script.py arguments注意 sys.argv 的长度为参数个数+1,不考虑前面的 “python”
交互解释器的初始化:
设置 PYTHONSTARTUP 变量,值为初始化文件 .py
也可以在初始化文件中执行其他文件,这样可以执行多个文件
注意,这种方式对 IDLE 无效。
初始化文件例子:
import os import sys if os.getcwd()== "C:\\Python26" : os.chdir(r"E:\python\workspace")
>>> os.path.exists(r"c:\\")
True
>>> os.path.isdir(r"c:\\")
True
>>> os.path.isfile(r"c:\\")
False
>>> os.path.split(r"C:\Python26\LICENSE.txt")
('C:\\Python26', 'LICENSE.txt')
>>> os.path.splitext(r"LICENSE.txt")
('LICENSE', '.txt')
>>> os.path.join('C:\\Python26', 'LICENSE.txt')
'C:\\Python26\\LICENSE.txt'
调用其他可执行程序,可以进行 stdin, stdout, stderr 的转向 import sys
import os
import subprocess
exe = r"E:\python\print-cmd.exe"
cmd = "%s -n %d -m %s"%(exe,100,'drink')
subprocess.call(cmd,stdout=None, )
or
file = open('result.txt', 'w')
subprocess.call(cmd, stdout = file)
file.close()
注意 cmd 中不能使用转向 '>',因为'>' 是 DOS 的东西。获得 python 版本
>>> import sys
>>> sys.version
'2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]'
>>> sys.version_info
sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0)
>>>
0 comments:
Post a Comment