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