[PYTHON] For Windows: Get a list of directories and files under a specific directory.

I just wanted to find something I wrote a script that scans directories on a Windows PC terminal all at once.

Below, in (1) of the script

mainDirList = os.listdir();

Just specify the target directory in the above argument and execute The process is written to write the list to the user directory. It may be updated soon.

import os
from io import StringIO
from io import BytesIO
import datetime


#Creating an io object
#Buffer the output
i = StringIO()
#Get the runtime directory
path = os.getcwd()

#Scan start source directory(1)
mainDirList = os.listdir("C:\\")

separate = "\\"
tab = "    "

def checkAllData(fileName, path, fp, tab = "    "):

    """Execute only when the first argument passed to the function is a directory"""
    """In this case, stop the process and scan the directory recursively."""
    if os.path. isdir(path) == True:
        fp.write(tab + fileName + "\n")
        directoryList = os.listdir(path)
        for __temp__ in directoryList :
            if os.path.isdir( path + separate + __temp__) == True:
                """Output parent directory"""
                fp.write(tab + __temp__ + "\n");
                res = (checkAllData(__temp__, path + separate + __temp__, fp, tab + "    "));
            else:
                fp.write(tab + "->" +__temp__ + "\n")
    else :
        """If the first argument passed to the function is a file, output as it is"""
        fp.write(tab + "->" + fileName + "\n")


"""Write directory structure to file"""
try:
    #Output the scanned directory list to a file.
    fp = open("C:\\Users\\Taro\\test.dat", "w", encoding="CP932")

    #Start scanning the directory
    for tempLine in mainDirList:
        try:
            """Loop through the current directory hierarchy"""
            path = "C:" + separate + tempLine
            if os.path.exists(path):
                fp.write(tempLine + "\n");
                checkAllData(tempLine, path, fp, "    " )
            else :
                fp.write("->" + tempLine + "\n");
        except Exception as e:
            print(str(e))
        except :
            fp.write("crush" + "\n");
    fp.close()
except Exception as e:
    print(str(e))

Recommended Posts

For Windows: Get a list of directories and files under a specific directory.
How to get a list of files in the same directory with python
Python: Get a list of methods for an object
[Command] Command to get a list of files containing double-byte characters
Get the number of specific elements in a python list
Python> Get a list of files in multiple directories> Use glob | Sort by modification time
A memorandum of files under conf.d
Get a list of CloudWatch Metrics and a correspondence table for Unit units with Python boto
Checks if there is a specific character string for all files under the directory that is Python and outputs the target line
Dig the directory and create a list of directory paths + file names
Get a list of files in a folder with python without a path
[python] Get a list of instance variables
Get the filename of a directory (glob)
[Python] Get a list of folders only
[Python] Create a list of date and time (datetime type) for a certain period
Get a list of IAM users with Boto3
Get a list of Qiita likes by scraping
Python script to get a list of input examples for the AtCoder contest
Get the value of a specific key in a list from the dictionary type in the list with Python
How to list files under the specified directory in a list (multiple conditions / subdirectory search)
Get a list of GA accounts, properties, and views as vertical data using API
List of Python libraries for data scientists and data engineers
A memo of installing Chainer 1.5 for GPU on Windows
Recursively search for files and directories in Python and output
(For myself) Flask_2 (list and for, extends, and a little more)
Linux: files and directories
Get the value of a specific key up to the specified index in the dictionary list in Python
Recursively get the Excel list in a specific folder with python and write it to Excel.
Check what the character code is for all files under the directory that is Python and output
[Python] Create a date and time list for a specified period
Installation procedure for Python and Ansible with a specific version
Get a list of purchased DMM eBooks with Python + Selenium
Since Python 1.5 of Discord, I can't get a list of members
Detect objects of a specific color and size with Python
How to get a list of built-in exceptions in python
A rough summary of the differences between Windows and Linux
Python: Create a dictionary from a list of keys and values
Get the number of occurrences for each element in the list
Mac Linux Check the capacity directly under a specific directory
How to get a list of links from a page from wikipedia
It is surprisingly troublesome to get a list of the last login date and time of Workspaces
python memo: enumerate () -get index and element of list at the same time and turn for statement
Linux (about files and directories)
Build and test a CI environment for multiple versions of Python
Try to get a list of breaking news threads in Python.
I made a lot of files for RDP connection with Python
I tried to get a list of AMI Names using Boto3
What is a dog? Volume of GET request and query parameters
Get a list of camera parameters that can be set with cv2.VideoCapture and make it a dictionary type