24
loading...
This website collects cookies to deliver better user experience
os.listdir()
to get all the files and directories in the specified path.# import OS module
import os
# List all the files and directories
path = "C:\Projects\Tryouts"
dir_list = os.listdir(path)
print("Files and directories in '", path, "' :")
# prints all files
print(dir_list)
Files and directories in ' C:\Projects\Tryouts ' :
['calc.py', 'etc', 'listindexerror.py', 'main.py', 'Python Tutorial.py', 'Python Tutorial.txt', 'test', 'test - Copy', ' __pycache__']
os.walk()
, which generates the files and folders in a directory tree. It can traverse the tree either top-down or bottom-up search, and by default, it sets as top-down search.os.walk()
also helps to retrieve the files and folder in the absolute path.# import OS module
import os
# List all the files and directories
path = "C:\Projects\Tryouts"
for (root, directories, files) in os.walk(path, topdown=False):
for name in files:
print(os.path.join(root, name))
for name in directories:
print(os.path.join(root, name))
C:\Projects\Tryouts\etc\password.txt
C:\Projects\Tryouts\test\python.txt
C:\Projects\Tryouts\test - Copy\python.txt
C:\Projects\Tryouts\ __pycache__ \calc.cpython-39.pyc
C:\Projects\Tryouts\calc.py
C:\Projects\Tryouts\listindexerror.py
C:\Projects\Tryouts\main.py
C:\Projects\Tryouts\Python Tutorial.py
C:\Projects\Tryouts\Python Tutorial.txt
C:\Projects\Tryouts\etc
C:\Projects\Tryouts\test
C:\Projects\Tryouts\test - Copy
C:\Projects\Tryouts\ __pycache__
os.scan()
method is available in Python 3.5 and above. scandir()
accepts either a bytes or str object for the path parameter and returns the DirEntry.name and DirEntry.path attributes with the same type as the path.# import OS module
import os
# List all the files and directories
path = "C:\Projects\Tryouts"
data = os.scandir()
for item in data:
if item.is_dir() or item.is_file():
print(item.name)
calc.py
etc
listindexerror.py
main.py
Python Tutorial.py
Python Tutorial.txt
test
test - Copy
__pycache__
glob
module helps you retrieve the files/path matching a specified pattern as the glob supports the wildcard search. We can get both files and folders using the glob module.# import OS module
import glob
# List all the files and directories
path = "C:\Projects\Tryouts\*"
for file_name in glob.iglob(path, recursive=True):
print(file_name)
C:\Projects\Tryouts\calc.py
C:\Projects\Tryouts\etc
C:\Projects\Tryouts\listindexerror.py
C:\Projects\Tryouts\main.py
C:\Projects\Tryouts\Python Tutorial.py
C:\Projects\Tryouts\Python Tutorial.txt
C:\Projects\Tryouts\test
C:\Projects\Tryouts\test - Copy
C:\Projects\Tryouts\ __pycache__
iglob()
method. All you need to do is set the recursive parameter as true.iglob()
method with recursive set to true and searching with a specific pattern to get all the .py files# import OS module
import glob
# List all the files and directories
path = "C:\Projects\Tryouts\*.py"
for file_name in glob.iglob(path, recursive=True):
print(file_name)
C:\Projects\Tryouts\calc.py
C:\Projects\Tryouts\listindexerror.py
C:\Projects\Tryouts\main.py
C:\Projects\Tryouts\Python Tutorial.py