following are some function used to accessing filename and directory using os module in python
1. scandir()
2. listdir()
3. walk()
- Iterator of DirEntry objects for given path
- When you not pass a path parameter defualt path='.' is passed to scandir() function which is current working path.
- To get the help documentation of os.scandir() use help() functiona and pass the os.scandir() as object
help(os.scandir)
Syntax:
os.scandir(path)
Example:
#import required module
import os
#os.scandir() return as iterator object
directory_data=os.scandir()
#print type of directory_data
print(type(directory_data))
#Result:<class 'posix.ScandirIterator'>
#iterate scandir object
for d in directory_data:
print(d)
Output:
<class 'posix.ScandirIterator'>
<DirEntry 'main.py'>
<DirEntry 'test.py'>
<DirEntry 'folderName'>
- Return a list containing the names of the files in the directory. The filenames returned will be str.
- If the path is None, uses the path='.'
Syntax:
os.listdir('path of directory')
Example:
#import required module
import os
#Get the current working path as a path to scan files and directory
path=os.getcwd()
#Get List of filenames avialable inside path
files=os.listdir(path)
print(files)
Output:
['main.py', 'test.py', 'folderName']
-
walk() is a Directory tree generator.
-
For each directory in the directory tree rooted at the top (including top itself, but excluding '.' and '..')
-
walk() Returns the 3 different tuple for
dirpath, dirnames, filenames
dirpath: This is a string path to the directory.
dirnames: Is the list of the subdirectories inside the dirpath. (excluding '.' and '..')
filename: Is the list of the name of the nondirectory files in dirpath
Note: that the filenames in the lists are just names, with no path components. you can get the full path of file name using
os.path.join(dirpath,filenames)
Syntax:
os.walk('path of Root Directory')
Example:
#import required module
import os
#Get Current directory path
path=os.getcwd()
#os.walk(path) return iteraable object
for dirpath, dirnames, filenames in os.walk(path):
print("*Directory path*")
print(dirpath)
print("\n***List of Directory*")
print(dirnames)
print("\n***List of files*")
print(filenames)
Output:
*Directory path*
/Demo/Python
*List of Directory*
[]
*List of files*
['main.py', 'test.py', 'folderName']
*Directory path*
/Demo/Python1
*List of Directory*
[]
*List of files*
['help.txt']