Python編程語言之os

Python編程語言之os

os —— Miscellaneous operating system interfaces

  • os的源碼位置:os源碼
  • 可以從源碼出發進行os的分析。

os分析

官方文檔說明

  • Python的API文檔給出的信息說明是:

    This module provides a portable way of using operating system dependent functionality. If you just want to read or write a file see open(), if you want to manipulate paths, see the os.path module, and if you want to read all the lines in all the files on the command line see the fileinput module. For creating temporary files and directories see the tempfile module, and for high-level file and directory handling see the shutil module.
    Notes on the availability of these functions:

    • The design of all built-in operating system dependent modules of Python is such that as long as the same functionality is available, it uses the same interface; for example, the function os.stat(path) returns stat information about path in the same format (which happens to have originated with the POSIX interface).
    • Extensions peculiar to a particular operating system are also available through the os module, but using them is of course a threat to portability.
    • All functions accepting path or file names accept both bytes and string objects, and result in an object of the same type, if a path or file name is returned.
    • An “Availability: Unix” note means that this function is commonly found on Unix systems. It does not make any claims about its existence on a specific operating system.
    • If not separately noted, all functions that claim “Availability: Unix” are supported on Mac OS X, which builds on a Unix core.
  • 還有給出的注意提示:

    Note All functions in this module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system.

文檔分析:

  • 首先os是Python編程語言中的一個模塊(module),這個模塊提供了一種使用操作系統的便攜式方式
  • 如果您只是想讀取(read)或者寫入(write)文件,可以使用Python編程語言的內置函數open(),如果要獲取操作時的路徑,就需要去參閱os.path模塊了,如果要讀取命令行中所有文件中的所有行,請參閱fileinput模塊了。有關創建臨時文件和臨時文件目錄的信息,請參閱tempfile模塊,有關高級文件和高級文件目錄的處理,請參閱shutil模塊了。
  • 關於這些功能的可用性的說明:
    • 所有內置操作系統相關的Python模塊的設計都是這樣的,只要相同的功能可用,它就使用相同的接口;例如,函數os.stat(path)以相同的格式(恰好源自POSIX接口)返回有關路徑的統計信息。
    • 特定操作系統特有的擴展也可以通過os模塊獲得,但使用它們當然是可移植性的威脅。
    • 接受路徑或者文件名的所有函數都接受字節和字符串對象,如果返回路徑或者文件名,則會生成相同類型的對象。
    • 一個”Availability: Unix”的提示表示此函數通常在Unix系統上找到。它沒有聲明它在特定操作系統上的存在。
    • 如果沒有進行特別說明,Mac OS X支持聲稱“Availability:Unix”的所有功能,它構建在Unix的核心之上。
  • os語義就是操作系統(Operate System),所以在Python編程語言中的os模塊的功能也是和操作系統關聯起來的,os模塊可以處理文件和目錄(這些處理內容需要手動進行處理的操作),就比如說:顯示當前路徑下所有文件/刪除某個文件/獲取文件大小等等。
  • 另外一個是os模塊不受任何操作系統平臺的影響,比如在linux下使用os.path.abspath(__file__)(返回當前路徑下的絕對路徑。在大多數平臺上,這相當於調用函數normpath(),如下所示:normpath(join(os.getcwd(), path))) ,在Windows下也同樣可以使用os,path.abspath(__file__)收穫相同的效果(獲取當前的絕對路徑)。

注意:如果文件名和路徑無效或無法訪問,或者其他參數類型正確但操作系統不接受,則此模塊中的所有函數都會引發“OSError”。


os模塊使用過的方法

os.name

The name of the operating system dependent module imported. The following names have currently been registered: posix, nt, os2, ce, java, riscos.
顯示當前使用的平臺

os.path.abspath(path)

這個在上面的已經分析過了,獲取當前路徑的絕對路徑顯示出來。

os.path.dirname(path)

Return the directory name of pathname path. This is the first element of the pair returned by passing path to the function split().
返回該路徑的父目錄

os.path.join(path, *paths)

Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.

On Windows, the drive letter is not reset when an absolute path component (e.g., r'\foo') is encountered. If a component contains a drive letter, all previous components are thrown away and the drive letter is reset. Note that since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.
連接目錄與文件名或者目錄,結果爲path/name

  • 測試用例
>>> os.name # 沒有引入os模塊,報出錯誤
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> import os
>>> os.name
'nt'    # Windows
>>> os.name
'posix'     # Linux
>>> os.path.dirname('test')
''      # 當初路徑下的父目錄
>>> os.path.dirname('C:\\projects\\workspace')
'C:\\projects'      # 當初路徑下的父目錄
>>> os.path.abspath('test')
'C:\\Users\\JackDan9\\test'     # 顯示test路徑的絕對路徑
>>> os.path.dirname(os.path.abspath('test'))
'C:\\Users\\JackDan9'   # 顯示test路徑的絕對路徑的父目錄
>>> os.path.join('/home/jackdan','test')
'/home/jackdan/test'
>>>

JackDan Thinking

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章