1

Following up with this question - is there a way to add multiple columns as the output?

Using column value as output path location python

C:/Desktop/main_folder/
file.txt

Bob   November  folder_1
Jon   January   folder_3
Sam   December  folder_1
Jane  April     folder_2

Modifying it to include columns 2 gives indexerror list out of range. Can you add a third column to the path variable?

with open('file.txt', 'r') as infile:
    for line in infile:
        columns = line.strip().split()  
        path = os.path.join('/Desktop/main_folder', columns[2], columns[1])  
        with open(path, 'w') as outfile:
            outfile.write(line)  
nora job
  • 35
  • 4

1 Answers1

2

Yes, you can use the output from the first join in an additional join.

path = os.path.join('/Desktop/main_folder', columns[1])
extended_path = os.path.join(path, columns[2])
Marcel Wilson
  • 3,842
  • 1
  • 26
  • 55