1. 程式人生 > >Python os 模組知識點整理

Python os 模組知識點整理

Python os模組知識點整理
>

先引用 os.py 的一段介紹

This exports:

  • all functions from posix, nt or ce, e.g. unlink, stat, etc.
    • os.path is either posixpath or ntpath
    • os.name is either ‘posix’, ‘nt’ or ‘ce’.
    • 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 (always ‘.’)
    • 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’ stand a better chance of being
portable between different platforms. Of course, they must then
only use functions that are defined by all platforms (e.g., unlink
and opendir), and leave all pathname manipulation to os.path
(e.g., split and join).

簡單說明:os 模組提供了一個統一的作業系統介面函式, 這些介面函式通常是平臺指定的,os 模組能在不同作業系統平臺如 nt 或 posix中的特定函式間自動切換,從而能實現跨平臺操作。

import os
1 檔案操作(常用)

os.path.split( path )
將path分割成目錄和檔名二元組

>>> path = 'd:\Learn\MATLAB\CH02\Fig0203(a).tif'
>>> os.path.split(path)
('d:\\Learn\\MATLAB\\CH02', 'Fig0203(a).tif')

os.path.splitext( path )
將path分割成’.’的字首和字尾

>>> os.path.splitext(path)
('d:\\Learn\\MATLAB\\CH02\\Fig0203(a)', '.tif')

os.path.basename( path )
返回path最後的檔名。

>>> os.path.basename(path)
'Fig0203(a).tif'

os.path.isfile( path )
如果path是一個存在的檔案,返回True。否則返回False

>>> os.path.isfile(path)
True

os.path.getsize( path )
返回path檔案的大小。’Bytes’

>>> os.path.getsize(path)
1146614

b = os.path.exists( path )
判斷檔案是否存在

>>> os.path.exists(path)
True

os.remove( file )
移除檔案file

os.rename( oldfileName, newFilename )
檔案重新命名

os.stat( )
獲取檔案/目錄資訊

>>> os.stat(path)
os.stat_result(st_mode=33206, st_ino=281474976710784, st_dev=1040830797, st_nlink=1, st_uid=0, st_gid=0, st_size=1146614, st_atime=1512965527, st_mtime=1053245450, st_ctime=1512965527)

2 目錄操作
os.listdir( path )
返回指定目錄下的所有目錄和檔案

os.getcwd()
獲取當前目錄

>>> os.chdir('d:\\')
>>> os.getcwd()
'd:\\'

os.makedirs( path )
建立子目錄,生成多層遞迴目錄

os.rmdir( path )
刪除子目錄

os.removedirs( )
若目錄為空則刪除該目錄,並遞迴到上一級目錄

os.chdir( path )
改變當前目錄到path,類似於cmd下的shell: cd

>>> os.chdir('d:\\')
>>> os.getcwd()
'd:\\'

os.curdir()
返回當前目錄

>>> os.curdir
'.'

os.walk()
生成一個目錄樹下的所有檔名

os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
top – 根目錄下的每一個資料夾(包含它自己), 產生3-元組 (dirpath, dirnames, filenames)【資料夾路徑, 資料夾名字, 檔名】。
topdown –可選,為True或者沒有指定, 一個目錄的的3-元組將比它的任何子資料夾的3-元組先產生 (目錄自上而下)。如果topdown為 False, 一個目錄的3-元組將比它的任何子資料夾的3-元組後產生 (目錄自下而上)。
onerror – 可選,是一個函式; 它呼叫時有一個引數, 一個OSError例項。報告這錯誤後,繼續walk,或者丟擲exception終止walk。
followlinks – 設定為 true,則通過軟連結訪問目錄。

os.path.join(path1[, path2[, …]])
多個路徑組合返回

os.write(fid, str)
寫入字串到檔案描述符fd中.返回實際寫入的字串長度。

os.renames(old, new)
遞迴地對目錄進行更名,也可以對檔案進行更名

3 其他

os.name
顯示當前使用的平臺

>>> os.name
'nt'

os.system()
開啟一個新的shell

>>> os.system('Hello')
1

os.sep
更改作業系統中的路徑分隔符

>>> os.sep
'\\'

os.linesep
字串給出當前平臺使用的行終止符

>>> os.linesep
'\r\n'

os.environ
獲取系統環境變數