22
33### Functions For Reading FileNames and Directory
44
5- following are the some function used to accessing filename and directory using os module in python
5+ following are some function used to accessing filename and directory using os module in python
6+ ```
671. scandir()
782. listdir()
893. walk()
10+ ```
911
1012#### 1.scandir()
1113
@@ -37,20 +39,19 @@ print(type(directory_data))
3739# iterate scandir object
3840for d in directory_data:
3941 print (d)
40-
41- '''
42- #Result
42+ ```
43+ Output:
44+ ```
4345<class 'posix.ScandirIterator'>
4446<DirEntry 'main.py'>
4547<DirEntry 'test.py'>
4648<DirEntry 'folderName'>
47- '''
4849```
4950
5051#### 2.listdir()
5152
5253- Return a list containing the names of the files in the directory. The filenames returned will be str.
53- - If path is None, uses the path='.'
54+ - If the path is None, uses the path='.'
5455
5556Syntax:
5657``` python
@@ -62,31 +63,33 @@ Example:
6263# import required module
6364import os
6465
65- # Get the current working path as path to scan files and directory
66+ # Get the current working path as a path to scan files and directory
6667path= os.getcwd()
6768
6869# Get List of filenames avialable inside path
6970files= os.listdir(path)
7071print (files)
71-
72- # Result:['main.py', 'test.py', 'folderName']
72+ ```
73+ Output:
74+ ```
75+ ['main.py', 'test.py', 'folderName']
7376```
7477
7578
7679#### 3.walk()
7780
7881- ** walk()** is a Directory tree generator.
79- - For each directory in the directory tree rooted at top (including top itself, but excluding '.' and '..')
82+ - For each directory in the directory tree rooted at the top (including top itself, but excluding '.' and '..')
8083- ** walk()** Returns the 3 different tuple for
8184 ```
8285 dirpath, dirnames, filenames
8386 ```
8487
85- **dirpath**:Is a string path to the directory.
88+ **dirpath**: This is a string path to the directory.
8689
87- **dirnames**:Is the list of the subdirectories inside the dirpath. (excluding '.' and '..')
90+ **dirnames**: Is the list of the subdirectories inside the dirpath. (excluding '.' and '..')
8891
89- **filename**:Is the list of the name of the non directory files in dirpath
92+ **filename**: Is the list of the name of the nondirectory files in dirpath
9093
9194 **Note**: that the filenames in the lists are just names, with no path components.
9295 you can get the full path of file name using
@@ -118,9 +121,9 @@ for dirpath, dirnames, filenames in os.walk(path):
118121
119122 print (" \n ***List of files*" )
120123 print (filenames)
121-
122- '''
123- #Result
124+ ```
125+ Output:
126+ ```
124127*Directory path*
125128/Demo/Python
126129
@@ -138,5 +141,4 @@ for dirpath, dirnames, filenames in os.walk(path):
138141
139142*List of files*
140143['help.txt']
141- '''
142144```
0 commit comments