sys模块

 

显示python 变量PYTHONPATH

sys.path

 

修改sys.path

sys.path.append(r'C:\mydir')

 

os模块

Shell variables  

os.environ

Running programs

os.system
os.popen
os.execv
os.spawnv

Spawning process

os.fork
os.pipe
os.waitpid
os.kill

Descriptor files,locks

os.open
os.read
os.write

File processing

os.remove
os.rename
os.mkfifo
os.mkdir
os.rmdir
os.getcwd
os.chdir
os.chmod
os.getpid
os.listdir
os.access

Portability tools

os.sep
os.pathsep
os.curdir
os.path.split
os.path.join

Pathname tools

os.path.exists('path')
os.path.isdir('path')
os.path.getsize('path)

 

获取属性

import os
dir(os)
dir(os.path)

 

获取当前目录

os.getcwd()

 

目录分隔符,path变量分隔符,行分隔符

os.sep,os.pathsep,os.linesep

 

判断path是不是目录

os.path.isdir(r'c:\users')

 

判断path是不是文件

os.path.isfile(r'c:\users\data.txt')

 

判断path是否存在

os.path.exists(r'c:\users')

 

分割path中的目录和文件

os.path.split(r'c\users\data.txt')

 

合并目录和文件path.join

os.path.join(r'c:\users','data.txt')

 

获取path中的目录部分,文件部分

>>> name = r'C:\temp\data.txt'
>>> os.path.dirname(name), os.path.basename(name)
('C:\\temp', 'data.txt')

 

分割文件扩展名

>>> os.path.splitext(r'C:\PP4thEd\Examples\PP4E\PyDemos.pyw')
('C:\\PP4thEd\\Examples\\PP4E\\PyDemos', '.pyw')

 

获取绝对路径

>>> os.path.abspath('') # empty string means the cwd
'C:\\Users'
>>> os.path.abspath('temp') # expand to full pathname in cwd
'C:\\Users\\temp'
>>> os.path.abspath(r'PP4E\dev') # partial paths relative to cwd
'C:\\Users\\PP4E\\dev'
>>> os.path.abspath('.') # relative path syntax expanded
'C:\\Users'
>>> os.path.abspath('..')
'C:\\'
>>> os.path.abspath(r'..\examples')
'C:\\examples'
>>> os.path.abspath(r'C:\PP4thEd\chapters') # absolute paths unchanged
'C:\\PP4thEd\\chapters'
>>> os.path.abspath(r'C:\temp\spam.txt')
'C:\\temp\\spam.txt'

 

运行shell命令

os.system    #Runs a shell command from a Python script
os.popen     #Runs a shell command and connects to its input or output streams

 

运行DOS命令(os.system)

C:\...\PP4E\System> python
>>> import os
>>> os.system('dir /B')
helloshell.py
more.py
more.pyc
spam.txt
__init__.py
0
>>> os.system('type helloshell.py')
# a Python program
print('The Meaning of Life')
0
>>> os.system('type hellshell.py')
Th

 

运行命令(os.popen)

>>> open('helloshell.py').read()
"# a Python program\nprint('The Meaning of Life')\n"
>>> text = os.popen('type helloshell.py').read()
>>> text
"# a Python program\nprint('The Meaning of Life')\n"
>>> listing = os.popen('dir /B').readlines()
>>> listing
['helloshell.py\n', 'more.py\n', 'more.pyc\n', 'spam.txt\n', '__init__.py\n']

 

os.system与os.popen功能对比

>>> os.system('python helloshell.py') # run a Python program
The Meaning of Life
0
>>> output = os.popen('python helloshell.py').read()
>>> output
'The Meaning of Life\n'

 

subprocess模块

subprocess可以实现os.system和os.popen相同的功能,而且在stream方面更具灵活性。

>>> import subprocess
>>> subprocess.call('python helloshell.py') # roughly like os.system()
The Meaning of Life
0
>>> subprocess.call('cmd /C "type helloshell.py"') # built-in shell cmd
# a Python program
print('The Meaning of Life')
0
>>> subprocess.call('type helloshell.py', shell=True) # alternative for built-ins
# a Python program
print('The Meaning of Life')
0

 

os.startfile

os.startfile("webpage.html") # open file in your web browser
os.startfile("document.doc") # open file in Microsoft Word
os.startfile("myscript.py") # run file with Python

 

os模块的一些其它工具

Fetches and sets shell environment variables

os.environ

Spawns a new child process on Unix-like systems

os.fork

Communicates between programs

os.pipe

Starts new programs

os.execlp

Starts new programs with lower-level control

os.spawnv

Opens a low-level descriptor-based file

os.open

Creates a new directory

os.mkdir

Creates a new named pipe

os.mkfifo

Fetches low-level file information

os.stat

Deletes a file by its pathname

os.remove

Applies a function or loop body to all parts of an entire directory tree

os.walk