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

1 comment:

winprofx.com said...

Unlock your trading potential with WinProFX, a platform designed to help traders navigate the financial markets with confidence. Whether you're just starting out or have years of trading experience, WinProFX offers advanced trading tools, real-time market insights, and a user-friendly interface to support smarter trading decisions. With a focus on reliability, security, and seamless execution, WinProFX empowers you to explore global markets, manage your investments efficiently, and work toward your financial goals with greater confidence.
Contact us Address – 1st Floor, The Sotheby Building, Rodney Bay, Gros-Islet, SAINT Lucia P.O Box 838, Castries, Saint Lucia Phone no – +97144471894 Website – https://winprofx.com/

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...