简明python教程 --C++程序员的视角(八):标准库
os模塊
這個模塊包含普遍的操作系統功能。
如果你希望你的程序能夠與平臺無關的話,這個模塊是尤為重要的。一個例子就是使用os.sep可以取代操作系統特定的路徑分割符。
| os.system() | 執行linux命令 >> os.system('ls -l')? ? >> subprocess.call('ls -l'.split()) |
| os.getcwd() | 得到當前工作目錄 |
| os.getenv()和os.putenv() | 讀取和設置環境變量 |
| os.listdir() | 返回指定目錄下的所有文件和目錄名 |
| os.remove() | 刪除文件 |
| os.system() | 運行shell命令 |
| os.path.split() | 返回一個路徑的目錄名和文件名? >>> os.path.split('/home/swaroop/byte/code/poem.txt')? ('/home/swaroop/byte/code', 'poem.txt') |
| os.path.isfile()和os.path.isdir() | 分別檢驗給出的路徑是一個文件還是目錄 |
| os.path.exists() | 檢驗給出的路徑是否真地存在 |
| os.linesep | 給出當前平臺使用的行終止符。? 例如,Windows使用'\r\n',Linux使用'\n'而Mac使用'\r' |
| os.name | 指示你正在使用的平臺。? 比如對于Windows,它是'nt',而對于Linux/Unix用戶,它是'posix'。 |
?
?
sys模塊
sys模塊包含系統對應的功能。
1. sys.argv
包含命令行參數,sys.argv[0]是當前運行的程序名稱
輸出:
?
2. sys.exit
退出正在運行的程序。和以往一樣,你可以看一下help(sys.exit)來了解更多詳情。
3. sys.stdin、sys.stdout和sys.stderr
它們分別對應你的程序的標準輸入、標準輸出和標準錯誤流。
?
數據結構
?
Tools for Working with Lists?里面介紹了collections.deque,bisect和heapq,都是很有用的數據結構
?
其他
?
這里的介紹相當有限,詳細的可以查閱The Python Standard Library和The Python Language Reference
- 10. Brief Tour of the Standard Library
- 10.1. Operating System Interface?(os,shutil)
- 10.2. File Wildcards?(glob)
- 10.3. Command Line Arguments?(sys,getopt,argparse)
- 10.4. Error Output Redirection and Program Termination?(sys)
- 10.5. String Pattern Matching?(re)
- 10.6. Mathematics?(math,random)
- 10.7. Internet Access?(urllib2,smtplib,poplib)
- 10.8. Dates and Times?(datetime)
- 10.9. Data Compression?(zlib,?gzip,?bz2,?zipfile?and?tarfile)
- 10.10. Performance Measurement?(timeit,profile?and?pstats)
- 10.11. Quality Control?(doctest,unittest)
- 10.12. Batteries Included
- Data interchange?csv,?xml.dom,?xml.sax
- Internationalization?gettext,?locale, and the?codecs
- Remote procedure calls?xmlrpclib,SimpleXMLRPCServer
- Managing email messages?email
- 11. Brief Tour of the Standard Library – Part II
- 11.1. Output Formatting?(repr,pprint,textwrap,locale)
- 11.2. Templating?(string.Template)
- 11.3. Working with Binary Data Record Layouts?(struct,read zip file directly)
- 11.4. Multi-threading?(threading)
- 11.5. Logging?(logging)
- 11.6. Weak References?(weakref)
- 11.7. Tools for Working with Lists?(array,collections.deque(),heapq)
- 11.8. Decimal Floating Point Arithmetic?(decimal.Decimal)
http://www.guokr.com/blog/480782/
http://docs.python.org/2/tutorial/stdlib.html
http://docs.python.org/2/tutorial/stdlib2.html
http://docs.python.org/2/contents.html
?
More Python resources:
- http://www.python.org: The major Python Web site. It contains code, documentation, and pointers to Python-related pages around the Web. This Web site is mirrored in various places around the world, such as Europe, Japan, and Australia; a mirror may be faster than the main site, depending on your geographical location.
- http://docs.python.org: Fast access to Python’s documentation.
- http://pypi.python.org: The Python Package Index, previously also nicknamed the Cheese Shop, is an index of user-created Python modules that are available for download. Once you begin releasing code, you can register it here so that others can find it.
- http://aspn.activestate.com/ASPN/Python/Cookbook/: The Python Cookbook is a sizable collection of code examples, larger modules, and useful scripts. Particularly notable contributions are collected in a book also titled Python Cookbook (O’Reilly & Associates, ISBN 0-596-00797-3.)
- from:?http://www.cnblogs.com/wei-li/p/3438713.html
總結
以上是生活随笔為你收集整理的简明python教程 --C++程序员的视角(八):标准库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简明python教程 --C++程序员的
- 下一篇: 简明python教程 --C++程序员的