1

I need to store the directory for each folder within holding_folder in a list.

C:\users\documents\holding_folder

within this folder I have hundreds of folders like:

Raw data 3543
Raw data 4565
Raw data 4677

however when I click on each folder there are empty folders until I get to the final folder with data.

example:

raw data 3543\sub\supersub\P3543

Folder P3543 has all the data. sub and supersub folders exist in each 'raw data xxxx folder'. The same 4 digits on the raw data xxxx are on the Pxxxx folder.

The first thing I did was pull the last 4 digits for each folder within the holding_folder using:

main_dir = os.listdir(r'C:\users\documents\holding_folder')
numeric_values = [x[-4:] for x in main_dir]

then I tried concatenating the string onto the two parts that I need to but received invalid syntax error:

final_paths= [(r'C:\users\documents\holding_folder' + str(x) + '\sub\supersub\P' + str(x)) for x in numeric_values]

I even tried something with os.path.join per recommendation here, but I am getting an invalid syntax error:

final_paths = [(os.path.join(r'C:\users\documents\holding_folder' + str(x) + '\sub\supersub\P' + str(x))) for x in numeric_values]

I also tried \\ instead of the singe \ but then each file path had two slashes. Example:

C:\\users\\documents\\holding_folder\\raw data 3543\\sub\\supersub\\P3543

my final desired output is this:

['C:\users\documents\holding_folder\3543\sub\supersub\P3543',
 'C:\users\documents\holding_folder\4565\sub\supersub\P4565',
 'C:\users\documents\holding_folder\4677\sub\supersub\P4677' ]

Any help with this would be great. I am not sure what I am missing.

I am ultimately trying to iterate through each file path. This is why I want to store each path in a list. using:

for item in list:
    os.chdir(item)
CandleWax
  • 2,159
  • 2
  • 28
  • 46

0 Answers0