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
i am presenting here, what are all i am reading and what are all experiences which I got. i am just sharing here. வணக்கம்....நான் விவேக்.. இங்கு எனக்கு கிடைத்த தகவலும் .. என்னை கவர்ந்த செய்திகளும் உங்களுக்கு தருகின்றேன் ...
Wednesday, March 13, 2024
How to Get files from the directory - One more method
Friday, October 13, 2023
How to get all file names and folders in directory? How to store to it in a spreadsheet ? Solution is here
Hi
I tried so many experiments to get the list of files and folders from a directory.
I was trying to get the files because I want to study and complete it.
Since it was a very huge task to get the files name from each directory and to store it manually in spreadsheet. I checked the Google AI, Bing AI to write a code in python.
After some struggles and mistakes, First time it wrote the file for small folders and very limited number for files.
So i took the help from Stakeoverflow (stakeoverflow) and it got solved.
Below I have given you the full code and sample.
Python code:-
import os
import openpyxl
import time
def list_files(directory):
"""Lists all files in the given directory and subdirectories.
Args:
directory: The directory to list.
Returns:
A list of all files in the given directory and subdirectories.
"""
allfiles = []
for root, _, files in os.walk(directory):
for file in files:
allfiles.append(os.path.join(root, file))
return allfiles
def save_to_spreadsheet(files, spreadsheet_path):
"""Saves the given list of files to the given spreadsheet.
Args:
files: A list of files.
spreadsheet_path: The path to the spreadsheet.
"""
start_time = time.time()
wb = openpyxl.Workbook()
ws = wb.active
# Write the header row.
ws.cell(row=1, column=1).value = "Index"
#ws.cell(row=1, column=2).value = "Header"
ws.cell(row=1, column=2).value = "Folder Name"
ws.cell(row=1, column=3).value = "Directory Path"
ws.cell(row=1, column=4).value = "File Path"
ws.cell(row=1, column=5).value = "File Name"
# ws.cell(row=1, column=7).value = "Time Taken (seconds)"
# Write the file data.
for i, file in enumerate(files):
ws.cell(row=i + 2, column=1).value = i + 1
#ws.cell(row=i + 2, column=2).value = "File"
ws.cell(row=i + 2, column=2).value = os.path.basename(os.path.dirname(file))
ws.cell(row=i + 2, column=3).value = os.path.dirname(file)
ws.cell(row=i + 2, column=4).value = file
ws.cell(row=i + 2, column=5).value = os.path.basename(file)
# Calculate the time taken to write the file.
#write_time = time.time() - start_time
#start_time = time.time()
#ws.cell(row=i + 2, column=7).value = write_time
# Save the spreadsheet.
wb.save(spreadsheet_path)
if __name__ == "__main__":
# Get the directory to list.
directory = "D:\\torrents\education\\100 Days of Code The Complete Python Pro Bootcamp for 2023"
# List the files in the directory and subdirectories.
files = list_files(directory)
# Save the file names to a spreadsheet.
spreadsheet_path = "D:\\torrents\education\\100 Days of Code The Complete Python Pro Bootcamp for 2023\\spreadsheet.xlsx"
save_to_spreadsheet(files, spreadsheet_path)
Tuesday, August 30, 2022
How to list all files in the folder and sub folder ( directories and sub directories) and store it in csv files
Hi Everyone,
I am newbie to Python #python
I have tried so many sites and could not able to find how to get files from the folder and its sub folder.
So i got bit and pieces from stakeoverflow forum and created this code with help of my friend Gowrishankar.
Please find the code below
code:-
import os
import pandas as pd
#path of the file you want to enemurate
path = "//home//halovivek//Downloads//education//Jimi Kwik - Super Brain//"
directory =[]
filename=[]
for (root,dirs, file) in os.walk(path):
for f in file:
directory.append(root)
filename.append(f)
print(f)
#column name of the sheet
df=pd.DataFrame(list(zip(directory,filename)),columns=['Directory',"filename"])
#change the file of exccl sheet
df.to_csv("filedirectory.csv")
(path:- if you are using windows then you need to use backward slash (\\) instead of forward slash - // )
Output:-
/home/halovivek/PycharmProjects/yearcoding/venv/bin/python /home/halovivek/PycharmProjects/yearcoding/30082022_listfilesfromdirectory.py
Day 12 - Implementation Day - Juggling Exercise.mp4
Day 12 - Implementation Day - Juggling Exercise.MP3
Part 2 - Preparing For The Quest.mp4
Part 1 - Welcome To Your 30-Day Superbrain Quest.mp4
Part 5 - 10 Morning Habits Geniuses Use To Jump Start The Brain.mp4
Part 3 - The FAST Method For Learning Anything.mp4
Part 2 - Preparing For The Quest.MP3
Part 1 - Welcome To Your 30-Day Superbrain Quest.MP3
Part 5 - 10 Morning Habits Geniuses Use To Jump Start The Brain.MP3
Part 4 - How To Take Notes.mp4
Part 3 - The FAST Method For Learning Anything.MP3
Part 4 - How To Take Notes.MP3
Day 27 - The Ancient Alphanumeric Code Of Memory Part 2 - Application.MP3
Day 27 - The Ancient Alphanumeric Code Of Memory Part 2 - Application.mp4
Day 29 - The 5 Levels Of Transformation.MP3
Day 29 - The 5 Levels Of Transformation.mp4
Day 14 - Memory Is As Easy As PIE.MP3
Day 14 - Memory Is As Easy As PIE.mp4
Day 15 - The FDR Technique.MP3
Day 15 - The FDR Technique.mp4
Day 31 - Overcoming Procrastination.mp4
Day 31 - Overcoming Procrastination.MP3
Day 9 - Chain Linking - Part 1.mp4
Day 9 - Chain Linking - Part 1.MP3
Day 8 - Implementation Day - Morning Routine.mp4
Day 8 - Implementation Day - Morning Routine.MP3
Day 24 - Implementation Day - Crossovers.MP3
Day 24 - Implementation Day - Crossovers.mp4
Day 19 - Learning Foreign Languages.mp4
Day 19 - Learning Foreign Languages.MP3
Day 13 - BE SUAVE Remembering Names.MP3
Day 13 - BE SUAVE Remembering Names.mp4
Day 34 - Speed Reading.MP3
Day 34 - Speed Reading.mp4
Day 22 - The Location Method.MP3
Day 22 - The Location Method.mp4
Day 5 - Nutrition _ Your Body Folders.MP3
Day 5 - Nutrition _ Your Body Folders.mp4
Day 23 - Memorize Word For Word.MP3
Day 23 - Memorize Word For Word.mp4
Day 30 - Q&A Session with Jim.mp4
Day 30 - The 5 Levels Of Learning.mp4
Day 30 - The 5 Levels Of Learning.MP3
Day 30 - Q&A with Jim.MP3
Day 28 - Implementation Day - Phonetic Number Code.MP3
Day 28 - Implementation Day - Phonetic Number Code.mp4
How To Become A Super Learner Masterclass - Jim Kwik.mp4
how_to_become_a_super_learner_by_jim_kwik_workbook_nsp.pdf
Day 18 - Keyword Substitution Method.MP3
Day 18 - Keyword Substitution Method.mp4
Day 32 - Your 8 C's To Muscle Memory.mp4
Day 32 - Your 8 C's To Muscle Memory.MP3
Day 4 - Implementation Day - Spaced Repetition Concept.mp4
Day 4 - Implementation Day - Spaced Repetition Concept.MP3
Day 26 - The Ancient Alphanumeric Code Of Memory Part 1 - The Sounds.MP3
Day 26 - The Ancient Alphanumeric Code Of Memory Part 1 - The Sounds.mp4
Day 2 - The Sun Is Up.mp4
Day 2 - The Sun Is Up.MP3
Day 16 - Implementation Day - Superbrain Yoga.MP3
Day 16 - Implementation Day - Superbrain Yoga.mp4
Day 10 - Chain Linking - Part 2.MP3
Day 10 - Chain Linking - Part 2.mp4
.getxfer.14810.259.mega
Day 7 - Sleep _ Stress Management.MP3
Day 7 - Sleep _ Stress Management.mp4
Day 33 - Remembering Your Dreams.mp4
Day 33 - Remembering Your Dreams.MP3
.getxfer.14709.10.mega
.getxfer.28947.5.mega
Day 21 - How To Give A Speech Without Notes.MP3
.getxfer.27388.5.mega
Day 3 - The 10 Keys To Unlocking Your Superbrain.mp4
.getxfer.10040.5.mega
Day 6 - Environments _ Killing ANTs.MP3
_Groupinsiders.com.url
Day 6 - Environments _ Killing ANTs.mp4
Day 25 - Numbers - The Basics.mp4
Day 25 - Numbers - The Basics.MP3
Day 1 - M.O.M. Can Help You Remember.MP3
Day 1 - M.O.M. Can Help You Remember.mp4
Day 20 - Implementation Day - Counting To 10 In Japanese.MP3
Day 20 - Implementation Day - Counting To 10 In Japanese.mp4
Day 11 - The Peg Memory Method.MP3
Day 11 - The Peg Memory Method.mp4
Day 17 - The Ultimate TIP To Remembering Anything.MP3
Day 17 - The Ultimate TIP To Remembering Anything.mp4
Process finished with exit code 0
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...
-
http://podian.blogspot.com/ One of the Good blog in Tamil so you can read current news also.
-
ஸ்ரீ இராம நாம மந்திர மகிமை 🌷 1. நமக்கு நன்மை வரவேண்டுமானால் 'ராம நாமத்தை இடைவிடாமல் கூறவேண்டும். நமது ஒவ்வொரு மூச்சும் ...
-
import os import openpyxl # Specify the target folder folder_path = "C:/Your/Target/Folder" # Replace with the actual path # Cre...
