Python中的OS模塊提供了與操作係統進行交互的函數。操作係統屬於Python的標準實用程序模塊。該模塊提供了使用依賴於操作係統的函數的便攜式方法。
os.scandir()
os模塊產量的方法os.DirEntry
與指定路徑給定目錄中的條目相對應的對象。os.DirEntry
對象具有各種屬性和方法,用於公開目錄條目的文件路徑和其他文件屬性。
is_symlink()
方法開啟os.DirEntry
object用於檢查條目是否為符號鏈接。
注意: os.DirEntry
打算在迭代後使用和丟棄對象,因為對象的屬性和方法將其值緩存起來,而不再重新獲取這些值。自調用os.scandir()方法以來,文件的元數據是否已更改,或者是否經過了很長時間。我們將不會獲得up-to-date信息。
用法: os.DirEntry.is_symlink()
參數:沒有
返回值:如果條目是符號鏈接(即使已斷開),則此方法返回True,否則返回False。
代碼1:使用os.DirEntry.is_symlink()
方法
# Python program to explain os.DirEntry.is_symlink() method
# importing os module
import os
# Directory to be scanned
# Path
path = "/home / ihritik"
# Print all symbolic links
# in the above path
# Using os.scandir() method
# scan the specified directory
# and yield os.DirEntry object
# for each file and sub-directory
obj = os.scandir(path)
print("Symbolic links in the path '% s':" % path)
for entry in obj:
# Check if the entry
# is a symbolic link
# using os.DirEntry.is_symlink() method
if entry.is_symlink():
# Print symbolic link
# full path
print(entry.path)
輸出:
Symbolic links in the path '/home/ihritik': /home/ihritik/file.txt /home/ihritik/sample.py
代碼:使用os.DirEntry.is_symlink()
方法
# Python program to explain os.DirEntry.is_symlink() method
# importing os module
import os
# Directory to be scanned
# Path
path = "/home / ihritik"
# Count number of
# symbolic links
# in the above path
# Using os.scandir() method
# scan the specified directory
# and yield os.DirEntry object
# for each file and sub-directory
obj = os.scandir(path)
count = 0;
for entry in obj:
# Check if the entry
# is a symbolic link
# using os.DirEntry.is_symlink() method
if entry.is_symlink():
count = count + 1
print("Count of symbolic links in the path '% s':" % path, count)
輸出:
Count of symbolic links in the path '/home/ihritik':2
參考文獻: https://docs.python.org/3/library/os.html#os.DirEntry.is_symlink
相關用法
- Python next()用法及代碼示例
- Python set()用法及代碼示例
- Python os.dup()用法及代碼示例
- Python os.WEXITSTATUS()用法及代碼示例
- Python os._exit()用法及代碼示例
- Python PIL UnsahrpMask()用法及代碼示例
- Python Numpy np.fft()用法及代碼示例
- Python os.abort()用法及代碼示例
- Python PIL RankFilter()用法及代碼示例
- Python os.WIFEXITED()用法及代碼示例
- Python os.setgroups()用法及代碼示例
- Python os.getcwd()用法及代碼示例
- Python os.sendfile()用法及代碼示例
- Python os.pipe2()用法及代碼示例
注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | os.DirEntry.is_symlink() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。