[Python] Scan the inside of the folder including subfolders → Export the file list to CSV

This is my first post. I recently started studying Python, and although I still have half-baked knowledge, I would like to post it as a memorandum. I would be grateful if you could feel free to comment on any advice.

Thing you want to do

I'm using a departmental shared folder in a corporate environment, but the capacity is small and it fills up quickly. I want to visualize what files are occupying space in this folder. As a general rule, using free software is NG according to company rules, so I made a tool myself after studying.

What I studied

1. 1. Directory scanning with walk module

You can easily scan the folder with os.walk. (In the case of VBA, it was necessary to perform recursive processing, but it can be easily written by using walk)

How to use

for curDir, dirs, files in os.walk(target_folder): For the folders described in target_folder_p above, the files are stored in files, the subfolders are stored in dirs, and the folders in which each is stored are stored in curDir. After that, further for nesting in that for, files and dirs are expanded, and processing is performed.

Example of use:

python


import os

target_folder = r"C:\Users\aaa\Desktop\Photo folder"

for curDir, dirs, files in os.walk(target_folder): 
    for a_file in files:
        print(f"{a_file}Is a folder:{curDir}It is stored in.")

result

Mandarin orange.jpg is a folder: C:\Users\aaa\Desktop\It is stored in the photo folder.
Apple.jpg is a folder: C:\Users\aaa\Desktop\It is stored in the photo folder.
banana.jpg is a folder: C:\Users\aaa\Desktop\It is stored in the photo folder.
dog.jpg is a folder: C:\Users\aaa\Desktop\Photo folder\Stored in animals.
Cat.jpg is a folder: C:\Users\aaa\Desktop\Photo folder\Stored in animals.

2. Output to csv file

For output to csv, open the file with the open function, convert it to csv with csv.writer, and write it with writerow or writerows.

Example of writing

python


import csv

with open (file name to output (full path),Read mode, newline="") as fo:
    CSV_file = csv.writer(fo)
    CSV_file.writerow(["A1", "B1", "C1", "D1"])
    CSV_file.writerow([["A2", "B2", "C2", "D2"],["A3", "B3", "C3", "D3"]])
    CSV_file.writerows([["A4", "B4", "C4", "D4"],["A5", "B5", "C5", "D5"]])

result: キャプチャ.PNG write row writes one line write rows write multiple lines at once

** Read mode (second argument of open function) **

I'm still studying the optional newline. However, if this is omitted, the data will be skipped by one line when it is output.

Deliverables

File list export.py


import os
import pathlib
import csv
from tkinter import filedialog

#Specify the folder to read
F_path = os.path.dirname(__file__)
target_folder = filedialog.askdirectory(initialdir=F_path)
target_folder_path = pathlib.Path(target_folder)

filedata = []

#Operate in the folder
for curDir, dirs, files in os.walk(target_folder_path):
    for a_File in files:

        file_size = os.path.getsize(os.path.join(curDir, a_File))
        file_ext = os.path.splitext(a_File)

        filedata.append([len(filedata)+1, a_File,file_ext[1], file_size,os.path.join(curDir, a_File)])

#Display reading result
print("No.File name Extension File size (Byte) Full path")

for i in range(len(filedata)):
    print(f"{filedata[i][0]}     {filedata[i][1]}     {filedata[i][2]}     {filedata[i][3]}     {filedata[i][4]}")

#Confirmation of file output destination
print("The file list is output as CSV to the top level of the read folder.\n Enter the output file name (no extension required)")
print("(If you do not want to output, leave blank and press ENTER)")
csv_name = input("Output file name:")

if csv_name == "":
    print("Did not output the file.")

else:
    #Output the read result to a file
    output_csv_path = os.path.join(target_folder_path, csv_name + ".csv")

    with open(output_csv_path, "w", newline="") as fo:
        CSV_file = csv.writer(fo)
        CSV_file.writerow(["No.", "file name","extension", "File size (Byte)","full path"])
        CSV_file.writerows(filedata)

    print(f"The following file was output.=> {output_csv_path}")

result

No.File name Extension File size (Byte) Full path
1 Mushroom Takeko War.jpg        .jpg           118437          C:\Users\aaa\Desktop\Photo folder\Mushroom Takeko War.jpg
2 Takenoko no Sato.jpg        .jpg           93003          C:\Users\aaa\Desktop\Photo folder\Takenoko no Sato.jpg
3 cat 1.jpg        .jpg           31115          C:\Users\aaa\Desktop\Photo folder\animal\Cat 1.jpg
4 cat 2.jpg        .jpg           21160          C:\Users\aaa\Desktop\Photo folder\animal\Cat 2.jpg
The file list is output as CSV to the top level of the read folder.
Enter the output file name (no extension required)
(If you do not want to output, leave blank and press ENTER)
Output file name: output
The following file was output.=> C:\Users\aaa\Desktop\Photo folder\output.csv

キャプチャ.PNG

Other memorandums / memos

Recommended Posts

[Python] Scan the inside of the folder including subfolders → Export the file list to CSV
Various ways to read the last line of a csv file in Python
Template of python script to read the contents of the file
I tried to touch the CSV file with Python
Try to get the function list of Python> os package
[Python] Open the csv file in the folder specified by pandas
[Python] Write to csv file with Python
Output to csv file with Python
About the basics list of Python basics
Python --Most elegant way to read lines of file into list
Output the contents of ~ .xlsx in the folder to HTML with Python
After calling the Shell file on Python, convert CSV to Parquet.
[Python] A program that rotates the contents of the list to the left
[Python] How to convert db file to csv
Change the length of Python csv strings
[Python] Convert csv file delimiters to tab delimiters
Check the existence of the file with python
Python> sys.path> List of strings indicating the path to search for modules
[Introduction to Python] How to sort the contents of a list efficiently with list sort
I tried to open the latest data of the Excel file managed by date in the folder with Python
How to put a line number at the beginning of a CSV file
[Python] How to read a csv file (read_csv method of pandas module)
Read Python csv and export to txt
Python amateurs try to summarize the list ①
Summary of how to use Python list
Python script that outputs all records of Oracle table to CSV file
[Python] Get the character code of the file
I made a program to check the size of a file in Python
[Python] Master the reading of csv files. List of main options for pandas.read_csv.
[Python3] Understand the basics of file operations
How to pass the execution result of a shell command in a list in Python
About the handling of ZIP files including Japanese files when upgrading from Python2 to Python3
Python script to get a list of input examples for the AtCoder contest
How to get a list of files in the same directory with python
How to read a CSV file with Python 2/3
[python] Check the elements of the list all, any
[Note] Export the html of the site with python.
[Introduction to Udemy Python 3 + Application] 19. Copy of list
Writing logs to CSV file (Python, C language)
[Algorithm x Python] How to use the list
How to get the files in the [Python] folder
Export Python3 version OpenCV KeyPoint to a file
List of Python code to move and remember
Convert the character code of the file with Python3
A python amateur tries to summarize the list ②
Speed evaluation of CSV file output in Python
Get the update date of the Python memo file.
How to identify the element with the smallest number of characters in a Python list?
How to check in Python if one of the elements of a list is in another list
Output the specified table of Oracle database in Python to Excel for each file
How to count the number of occurrences of each element in the list in Python with weight
Convert financial information of all listed companies for the past 5 years to CSV file
Easy way to check the source of Python modules
[Python] How to remove duplicate values from the list
The wall of changing the Django service from Python 2.7 to Python 3
How to get the number of digits in Python
List of posts related to optimization by Python to docker
[Python] Get the official file path of the shortcut file (.lnk)
[python] Get the list of classes defined in the module
How to write a list / dictionary type of Python3
Read the xml file by referring to the Python tutorial