日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python標準庫os常用函數和屬性詳解_python

作者:Albert?Darren ? 更新時間: 2022-12-26 編程語言

1. OS標準庫簡介

顧名思義,OS表示Operating System,即操作系統。OS標準庫是一個操作系統接口模塊,提供一些方便使用操作系統相關功能的函數,具體安裝位置可通過導入os模塊查看os.__file__屬性得到。當需要在Python代碼中調用OS相關功能實現業務邏輯或者無法直接使用命令行工具時,我們就需要考慮導入此模塊,因此有必要進行深入學習。

2. OS標準庫常用函數和屬性

2.1 文件和目錄

2.1.1 os.getcwd()

返回表示當前工作目錄的字符串

print("當前工作目錄為:{}".format(os.getcwd())) # 返回當前工作目錄

2.1.2 os.mkdir(path, mode=0o777, *, dir_fd=None)

以指定數字表示的權限模式mode創建一個名為path的目錄。某些系統會忽略 mode,如果沒有忽略,那么Linux系統來說,新建文件夾的權限=指定數字表示的權限模式mode-當前系統用戶的umask默認權限,如下所示

"""
Linux操作系統可通過umask命令獲得4個八進制數表示的默認權限,root用戶默認是0022,普通用戶默認是 0002
第1位數代表文件所具有的特殊權限(SetUID、SetGID、Sticky BIT),后3位數表示表示umask權限值
分別對應所有者、用戶組、其他人的權限值,權限與數字對應關系為:r->4,w->2,x->1
"""
exit_code=os.system("umask")

"""
文件夾模式mode賦值為十進制511,等價于八進制0o777
"""
set_mode=511
os.mkdir(path="./cyr",mode=set_mode) # 在當前目錄創建名為cyr的文件夾
# 長格式查看新創建的文件夾cyr可知其權限字符串為rwxr-xr-x,等價于轉換后的數字權限111101101
!ls -l | grep cyr

umask_value=0o0022 # 當前系統用戶八進制表示umask默認權限
new_dir_mode=set_mode-umask_value
print("新建文件夾的權限為:{:b}".format(new_dir_mode))

os.rmdir(path, *, dir_fd=None)

移除(刪除)目錄 path。如果目錄不存在或不為空,則會分別拋出 FileNotFoundErrorOSError 異常。

os.rmdir("./cyr") # 刪除空文件夾成功,無法查到cyr目錄
!ls | grep cyr
os.rmdir("./why") # 刪除不存在的文件夾FileNotFoundError報錯

os.rmdir("./nnunet/") # 刪除不為空文件夾OSError報錯

os.chdir(path)

將當前工作目錄更改為 path

print("切換前的當前工作目錄為:{}".format(os.getcwd())) # 返回當前工作目錄
dst_path="/root" # 目標文件夾
os.chdir(dst_path) # 將當前工作目錄切換為/root
print("切換后的當前工作目錄為:{}".format(os.getcwd())) # 返回當前工作目錄

os.listdir(path='.')

返回一個包含指定path下所有文件和目錄名稱的按任意順序排列的列表,特殊條目’.‘和’…'除外

dst_path="/code/" # 目標目錄
dirs_ls=os.listdir(path=dst_path) # 獲得指定目錄下全部文件和文件夾名稱列表
print(dirs_ls)

2.2 os.path常見路徑操作

2.2.1 os.path.abspath(path)

返回路徑path 的絕對路徑(標準化的),相當于字符串拼接,路徑path不存在也不會報錯

relative_path="tests/test_steps_for_sliding_window_prediction.py" # 路徑path存在
print("{}對應的絕對路徑為{}".format(relative_path,os.path.abspath(relative_path)))

no_path="tests/none.py" # 路徑path不存在
print("{}對應的絕對路徑為{}".format(relative_path,os.path.abspath(no_path)))

2.2.2 os.path.basename(path)

返回路徑 path 的基本名稱

full_pathname="/proc/bus/pci/3a/08.0" # 路徑path存在
print("全路徑名稱對應的文件名為{}".format(os.path.basename(full_pathname)))

no_full_pathname="/demo/none.cpp" # 路徑path不存在
print("全路徑名稱對應的文件名為{}".format(os.path.basename(no_full_pathname)))

2.2.3 os.path.dirname(path)

返回路徑 path 的目錄名稱

full_pathname="/proc/bus/pci/3a/08.0" # 路徑path存在
print("全路徑名稱對應的目錄名稱為{}".format(os.path.dirname(full_pathname)))

no_full_pathname="/demo/none.cpp" # 路徑path不存在
print("全路徑名稱對應的目錄名稱為{}".format(os.path.dirname(no_full_pathname)))

2.2.4 os.path.exists(path)

判斷path是否指向一個已存在路徑或已打開的文件描述符,存在返回True,不存在返回False

full_pathname="/proc/bus/pci/3a/08.0" # 路徑path存在
print("全路徑名稱對應的目錄是否存在?{}".format(os.path.exists(full_pathname)))

no_full_pathname="/demo/none.cpp" # 路徑path不存在
print("全路徑名稱對應的目錄是否存在?{}".format(os.path.exists(no_full_pathname)))

2.2.5 os.path.isabs(path)

判斷path是否為一個絕對路徑,是則返回True,不是或不存在則返回False。在 Unix 上,它就是以斜杠開頭,而在 Windows 上,它可以是去掉驅動器號后以斜杠(或反斜杠)開頭。

abs_pathname="/proc/bus/pci/3a/08.0" # 路徑path存在
print("全路徑名稱對應的目錄是否為絕對路徑?{}".format(os.path.isabs(abs_pathname)))

rel_pathname="./nnunet/__init__.py" # 路徑path是相對路徑
print("全路徑名稱對應的目錄是否絕對路徑?{}".format(os.path.isabs(rel_pathname)))

no_pathname="./nnunet/none.py" # 路徑path是不存在
print("全路徑名稱對應的目錄是否絕對路徑?{}".format(os.path.isabs(no_pathname)))

2.2.6 os.path.isfile(path)

若path為指向一個已存在文件的符號鏈接或一個已存在文件路徑,返回True。若path為一個文件夾路徑或不存在路徑,返回False。

ls -li /opt/conda/bin/python* # 帶inode節點信息并長格式查看python開頭的文件和文件夾

由上圖可發現/opt/conda/bin/python為一個符號鏈接(軟鏈接)指向一個已存在文件路徑/opt/conda/bin/python3.7

abs_pathname="/opt/conda/bin/python3.7" # path為一個已存在文件路徑
print("全路徑名稱對應的文件是否存在?{}".format(os.path.isfile(abs_pathname)))

symbolic_link="/opt/conda/bin/python" # path為指向一個已存在文件/opt/conda/bin/python3.7的符號鏈接
print("全路徑名稱對應的文件是否存在?{}".format(os.path.isfile(symbolic_link)))

abs_path="/opt/conda/bin/" # 文件夾路徑
print("全路徑名稱對應的文件是否存在?{}".format(os.path.isfile(abs_path)))

no_full_pathname="/demo/none.cpp" # 路徑path不存在
print("全路徑名稱對應的文件是否存在?{}".format(os.path.isfile(no_full_pathname)))

2.2.7 os.path.isdir(path)

若path為指向一個已存在文件夾的符號鏈接或一個已存在文件夾路徑,返回True。若path為一個文件路徑或不存在路徑,返回False。

ls /code/nnunet/ # 查看已存在文件夾路徑/code/nnunet/

ln -s /code/nnunet/ ./symlink2codennunet # 當前目錄即root下創建一個軟鏈接指向一個已存在文件夾路徑/code/nnunet/
ls -l /root/

由上圖可知root用戶主目錄下存在一個軟鏈接symlink2codennunet指向一個已存在文件夾路徑

exist_dir_path="/code/nnunet/"# path為一個已存在文件夾路徑
print("全路徑名稱對應的文件夾是否存在?{}".format(os.path.isdir(exist_dir_path)))

exist_dir_symlink="/root/symlink2codennunet/"# path為指向一個已存在文件夾的符號鏈接
print("全路徑名稱對應的文件夾是否存在?{}".format(os.path.isdir(exist_dir_symlink)))

exist_file_path="/opt/conda/bin/python3.7"# path為一個已存在文件路徑
print("全路徑名稱對應的文件夾是否存在?{}".format(os.path.isdir(exist_file_path)))

no_path="/demo/none.cpp" # 路徑path不存在
print("全路徑名稱對應的文件夾是否存在?{}".format(os.path.isdir(no_path)))

2.2.8 os.path.islink(path)

若path代表一個已存在的符號鏈接,則返回True,反之則返回False。如果 Python 運行時不支持符號鏈接,則總是返回 False

exist_symbolic_link="/opt/conda/bin/python" # path為指向一個已存在的符號鏈接
print("全路徑名稱對應的符號鏈接是否存在?{}".format(os.path.islink(exist_symbolic_link)))

no_symbolic_link="/demo/no_link" # path為指向一個不存在的符號鏈接
print("全路徑名稱對應的符號鏈接是否存在?{}".format(os.path.islink(no_symbolic_link)))

exist_file_path="/opt/conda/bin/python3.7"# path為一個已存在文件路徑
print("全路徑名稱對應的符號鏈接是否存在?{}".format(os.path.islink(exist_file_path)))

exist_dir_path="/root/"# path為一個已存在文件夾路徑
print("全路徑名稱對應的符號鏈接是否存在?{}".format(os.path.islink(exist_dir_path)))

2.2.9 os.path.join(path, *paths)

拼接兩個或多個路徑部分,按需要插入/。如果參數中某個部分是絕對路徑,則絕對路徑前的路徑都將被丟棄,并從絕對路徑部分開始連接。如果最后一部分為空,則結果將以分隔符結尾。

previous_path,abs_dirname,basename,empty_part="model","/code","demo.py",""
print("參數中某個部分是絕對路徑拼接后為{}".format(os.path.join(previous_path,abs_dirname,basename)))

print("拼接兩個或多個路徑部分,按需要插入'/'拼接后為{}".format(os.path.join(previous_path,basename)))

print("最后一部分為空以分隔符結尾{}".format(os.path.join(previous_path,basename,empty_part)))

2.2.10 os.path.normcase(path)

規范路徑名稱的大小寫。 在 Windows 上,將路徑名稱中的所有字符轉為小寫,并將正斜杠轉為反斜杠。 在其他操作系統上,將路徑不加修改地返回。

Linux操作系統

print("當前操作系統模塊名為:{}".format(os.name))
windows_style_path=r"C:/Users\defaultuser0/APPData"
print("Windows路徑規范化后為{}".format(os.path.normcase(windows_style_path)))

Windows操作系統

2.2.11 os.path.split(path)

將路徑 path 拆分為一對,即 (head, tail),其中,tail 是路徑的最后一部分,而 head 里是除最后部分外的所有內容。tail 部分不會包含斜杠,如果 path 以斜杠結尾,則 tail 將為空。如果 path 中沒有斜杠,head 將為空。如果 path 為空,則 head 和 tail 均為空。head 末尾的斜杠會被去掉,除非它是根目錄(即它僅包含一個或多個斜杠)。

norm_path="/nnunet/configuration.py" # 一般路徑
ends_with_slash_path="/code/nnunet/" # 以斜杠結尾的路徑
no_slash_path="HIP_Logo.png" # 沒有斜杠的路徑
empty_path="" # 空路徑
root_path="/" # 根目錄
print("一般路徑head={},tail={}".format(*os.path.split(norm_path)))
print("以斜杠結尾的路徑head={},tail={}".format(*os.path.split(ends_with_slash_path)))
print("沒有斜杠的路徑head={},tail={}".format(*os.path.split(no_slash_path)))
print("空路徑head={},tail={}".format(*os.path.split(empty_path)))
print("根目錄head={},tail={}".format(*os.path.split(root_path)))

2.2.12 os.path.splitext(path)

將路徑 path 拆分為一對,即 (root, ext),使 root + ext == path,其中 ext 為空或以英文句點開頭,且最多包含一個句點。路徑前的句點將被忽略,例如 splitext(‘.cshrc’) 返回 (‘.cshrc’, ‘’)。

dir_path="/code/nnunet/" # 文件夾路徑
multi_dot_file_path="/code/i.thy.py" # 包含多個句點的文件路徑
single_dot_file_path="/code/we.py" # 包含單個句點的文件路徑
starts_with_dot_file_path=".bashrc" # 以句點開頭的路徑
print("文件夾路徑root={},ext={}".format(*os.path.splitext(dir_path)))
print("包含多個句點的文件路徑root={},ext={}".format(*os.path.splitext(multi_dot_file_path)))
print("包含單個句點的文件路徑root={},ext={}".format(*os.path.splitext(single_dot_file_path)))
print("以句點開頭的路徑root={},ext={}".format(*os.path.splitext(starts_with_dot_file_path)))

2.3 其他常用命令

2.3.1 os.name

導入的依賴特定操作系統的模塊的名稱,返回’posix’表示Linux,'nt’表示Windows,'java’表示Java虛擬機

print("當前操作系統平臺名稱為{}".format(os.name))

2.3.2 os.__file__

以字符串形式返回os模塊安裝的絕對路徑

     import os
     print("os模塊安裝絕對路徑是{}".format(os.__file__))

3. 參考文獻

  • os — 操作系統接口模塊 — Python 3.7.13 文檔
  • os.path — 常見路徑操作 — Python 3.7.13 文檔
  • Linux umask詳解:令新建文件和目錄擁有默認權限 (biancheng.net)
  • Linux chmod命令:修改文件或目錄的權限 (biancheng.net)

原文鏈接:https://blog.csdn.net/m0_46223009/article/details/128065092

欄目分類
最近更新