I have a program that I want to make more dynamic. The current setup is literally typing everything out.
I would like the program to work with a for loop (any other suggestions would be great). My goal is to loop through a specific file that has sub-directories and get the name of each folder (sub-directory), then get the name of the files within the sub-directory.
To put this in a file string: C:\Folder 1\Folder 2\File name
From the above, I would like to get the value of Folder 2
and File name
.
My code so far:
for sub_dir in os.listdir(r"C:\Folder 1\"):
DIR = r'' + sub_dir
files_found = len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])
if(files_found > 0):
for files in os.listdir(sub_dir):
file_name = os.path.splitext(files)[0]
I get the error --> FileNotFoundError: [WinError 3] The system cannot find the path specified: Folder 2
I appreciate your efforts to help.
You should take a look at os.walk