Reading Time: 1 minutes
List all text files in the current directory in Python
List all text files in the current directory: Write a Python program to list all text files i.e. .txt files present in the current directory.
List all text files in the current directory
import glob print( glob.glob('*.txt') ) # returns a list of all .txt files in the current directory # In order to check which is the current directory: import os print( os.getcwd() ) # To change current directory: import os os.chdir(path) # e.g. os.chdir(r'C:\Users\')
For more on glob, click here.