Wednesday, March 13, 2024

How to Get files from the directory - One more method

 import os
import openpyxl

# Specify the target folder
folder_path = "C:/Your/Target/Folder"  # Replace with the actual path

# Create a new Excel workbook
workbook = openpyxl.Workbook()
sheet = workbook.active

# Set column headers
sheet['A1'] = 'Serial Number'
sheet['B1'] = 'Path'
sheet['C1'] = 'Folder Name'
sheet['D1'] = 'File Name'

# Assign a unique serial number to each file
row_num = 2  # Start writing data from row 2

def list_files(path):
    global row_num
    for root, directories, files in os.walk(path):
        for file in files:
            full_path = os.path.join(root, file)
            folder_name = os.path.basename(root)
            sheet.cell(row=row_num, column=1).value = row_num - 1  # Serial number
            sheet.cell(row=row_num, column=2).value = full_path
            sheet.cell(row=row_num, column=3).value = folder_name
            sheet.cell(row=row_num, column=4).value = file
            row_num += 1

list_files(folder_path)

# Save the Excel workbook
workbook.save("file_list.xlsx")  # Adjust the filename as needed

How to Get files from the directory - One more method

 import os import openpyxl # Specify the target folder folder_path = "C:/Your/Target/Folder"  # Replace with the actual path # Cre...