Python | os.DirEntry.path attribute
Last Updated :
24 Jan, 2024
Path
attribute of os.DirEntry
object is used to get the entry’s full path name. The full path is absolute only if the path parameter used in the method is absolute. Also if the method path parameter was a file descriptor then the value of os.DirEntry.path
the attribute is the same as os.DirEntry.name
the attribute.
os.scandir()
method of Python OS Module yields os.DirEntry
objects corresponding to the entries in the directory given by a specified path. os.DirEntry or os direntry
object has various attributes and methods which is used to expose the file path and other file attributes of the directory entry.
Note: os.DirEntry
objects are intended to be used and thrown away after iteration as attributes and methods of the object cache their values and never prefetch the values again. If the metadata of the file has been changed or if a long time has elapsed since calling os.scandir() method. we will not get up-to-date information.
os.DirEntry.path attribute Syntax in Python
Syntax: os.DirEntry.path
Parameter: None
Return value: This attribute returns a bytes value if os.scandir() path parameter is bytes otherwise returns a string value which represents the entry’s full path.
Python os.DirEntry.path attribute Example
Below, we explain the example of a Python directory. those are follows
Use of OS DirEntry Attribute
In this example, below Python code uses `os.scandir()` to loop through entries in a directory. For each file entry, it checks if it’s a file with `entry.is_file()` and prints the file name (`entry.name`) and full path (`entry.path`).
Python3
import os
directory_path = '/path/to/your/directory'
for entry in os.scandir(directory_path):
if entry.is_file():
print (f "File: {entry.name}, Path: {entry.path}" )
|
Output:
Assuming you replace '/path/to/your/directory'
with the path to a directory containing files, the output will be something like:
File: file1.txt, Path: /path/to/your/directory/file1.txt
File: file2.txt, Path: /path/to/your/directory/file2.txt
File: file3.txt, Path: /path/to/your/directory/file3.txt
Use of Python DirEntry
Attribute
In this example , the Python code uses the os.scandir()
method to scan and print the full path of all directory entries (files and sub-directories) in the current working directory. It excludes entries starting or Python direntry with a dot (‘.’) to filter out hidden files or directories.
Python3
import os
path = os.getcwd()
print ( "Full path of all directory entry in '% s':" % path)
with os.scandir(path) as itr:
for entry in itr :
if not entry.name.startswith( '.' ) :
print (entry.name, ":" , entry.path)
|
Output
Full path of all directory entry in '/home/ihritik':
Public : /home/ihritik/Public
Desktop : /home/ihritik/Deskop
R : /home/ihritik/R
foo.txt : /home/ihritik/foo.txt
graph.cpp : /home/ihritik/graph.cpp
tree.cpp : /home/ihritik/tree.cpp
Pictures : /home/ihritik/Pictures
abc.py : /home/ihritik/abc.py
file.txt : /home/ihritik/file.txt
Videos : /home/ihritik/Videos
images : /home/ihritik/images
Downloads : /home/ihritik/Downloads
GeeksforGeeks : /home/ihritik/GeeksforGeeks
Music : /home/ihritik/Music
Documents : /home/ihritik/Documents
FAQ’s
Q.How does the @property decorator work in Python?
@property
decorator in Python allows the definition of a method to be accessed as an attribute. It transforms a method into a read-only property, enabling direct access without invoking the method.
Share your thoughts in the comments
Please Login to comment...