os模块1 [Python]
常用的os模块工具
Tasks
tools
Shell variables
os.environ
Running programs
os.system
os.popen
os.popen2/3/4
os.startfile
Spawning processes
os.fork
os.pipe
os.exec
os.waitpid
os.kill
Descriptor files, locks
os. open
os.read
os.write
File processing
os.remove
os.rename
os.mkfifo
os.mkidr
os.rmdir
Administrative tools
os.getcwd
os.chdir
os.chmod
os.getpid
os.listdir
Portability tools
os.sep
os.pathsep
os.curdir
os.path.split
os.path.join
Pathname tools
os.path.exisets('path')
os.path.isdir('path')
os.path.getsize('path')
os模块的方法和属性是比较多的,而且在不同的平台下,也不尽相同.
Linux
[root@lvdbing ~]# python
Python 2.5.2 (r252:60911, Sep 26 2008, 01:57:27)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> dir(os)
['EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', ... ... ... 'umask', 'write']
>>> len(dir(os))
213
WindowsXP
Python 2.5.2 (r252:60911, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> dir(os)
['F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', ... ... ... , 'write']
>>> len(dir(os))
122
可以看到Linux和WindowsXP是不同的.
如何才能知道模块提供的工具如何使用呢,Python使用help()函数可以实时查看帮助文档.
>>> help(os)
Help on module os:
NAME
os - OS routines for Mac, NT, or Posix depending on what system we're on.
FILE
/usr/local/python25/lib/python2.5/os.py
MODULE DOCS
http://www.python.org/doc/current/lib/module-os.html
DESCRIPTION
This exports:
- all functions from posix, nt, os2, mac, or ce, e.g. unlink, stat, etc.
- os.path is one of the modules posixpath, ntpath, or macpath
- os.name is 'posix', 'nt', 'os2', 'mac', 'ce' or 'riscos'
- os.curdir is a string representing the current directory ('.' or ':')
- os.pardir is a string representing the parent directory ('..' or '::')
- os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')
- os.extsep is the extension separator ('.' or '/')
- os.altsep is the alternate pathname separator (None or '/')
- os.pathsep is the component separator used in $PATH etc
- os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
- os.defpath is the default search path for executables
- os.devnull is the file path of the null device ('/dev/null', etc.)
Programs that import and use 'os