Python Code to List all Files from Desired Folder
To get list of files present in a computer folder to a string variable, use the code below.
Edit the folder path mentioned in the variable “folder_path”. Then execute this ready to use code.
It will print the file name one by one.
Here is the code List Files:Python Code - List Files in DirectoryPython12345678910111213141516171819#import Librariesimport os,sys#Enter Folder Pathfolder_path = 'C:/'#To Get Folder path from Command lineif len(sys.argv) == 2:folder_path = sys.argv#Get File details to a listfile_list = os.listdir(folder_path)#Read File one by onefor file_name in file_list:print(file_name)#Program Endprint("Program Completed")
The output will look something like this:
- Program completed
- File List by Extension or Type:
Python – List Files Filtered by File Type
This code does not fetch the file names filtered by extension or file type.
To do that, include this if condition just below the for loop.
This ensures that only the files ending with this extension are picked up for processing.
Python – List files in a Directory – Filter by File Type
- Block Apps in FaceBook print screen Excel Countif Record Macro Timer in Excel Task Manager VCF HTML Table Import Data Thumbnails Text to Speech TTS Candy Crush C++ Programing Machine learning AI bulk email Excel VBA vba color index vba color codes Android social media Twitter Google+ Google Adsense Tips bitcoin Calendar in Excel outlook vba mass email Data Mining Excel Macro Facebook WhatsApp Wordpress python