Skip to content

Commit b65930d

Browse files
authored
Update AccessFileNames.md
1 parent 65c89f6 commit b65930d

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

Module/os/AccessFileNames.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
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+
```
67
1. scandir()
78
2. listdir()
89
3. walk()
10+
```
911

1012
#### 1.scandir()
1113

@@ -37,20 +39,19 @@ print(type(directory_data))
3739
#iterate scandir object
3840
for 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

5556
Syntax:
5657
```python
@@ -62,31 +63,33 @@ Example:
6263
#import required module
6364
import 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
6667
path=os.getcwd()
6768

6869
#Get List of filenames avialable inside path
6970
files=os.listdir(path)
7071
print(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

Comments
 (0)