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
os.remove
os.walk