When working on files , Sometimes you need to get the file names of all files in the current folder , If obtained “E:\ Worksheet \ data statistics \ Website log \ journal ”, Names of all files in this directory

stay Windows Under the system command line window , It is used to view all file names under the current folder dir, Therefore, it needs to be used in the code os modular

code implementation
# Import os modular import os # path Directory that defines the file name to get path = "E:\ Worksheet \ data statistics \ Website log \ journal " #
os.listdir() Method to get the folder name , Return array file_name_list = os.listdir(path) # Convert to string
file_name = str(file_name_list) # replace replace "[","]"," ","'" file_name =
file_name.replace("[", "").replace("]", "").replace("'", "").replace(",",
"\n").replace(" ", "") # Create and open files list.txt f = open(path + "\\" + " file list.txt",
"a") # Write the name under the file to " file list.txt" f.write(file_name)
Output results