1

I am trying to get the file names which are in a folder wrote into an output file with the index of each line. I am using the code below to write the filenames:

import os

with open("bionaplsatfiles.dat", "w") as a:
    for path, subdirs, files in os.walk(r'D:\Bionapl_Nicolas\testKB\vtu_files\output_Sn'):
       for filename in files:
         f = os.path.join(filename)
         a.write(str(f) + os.linesep)

I am getting the result below:

Sat_t0.txt
Sat_t1.txt
Sat_t2.txt
Sat_t3.txt
Sat_t4.txt
Sat_t5.txt

But what I need is :

0   Sat_t0.txt
1   Sat_t1.txt
2   Sat_t2.txt
3   Sat_t3.txt
4   Sat_t4.txt
5   Sat_t5.txt

What should I add to my code to get the index column? Thank you

Joëlle
  • 47
  • 5

2 Answers2

1

Use enumerate:

import os

with open("bionaplsatfiles.dat", "w") as a:
    for path, subdirs, files in os.walk(r'D:\Bionapl_Nicolas\testKB\vtu_files\output_Sn'):
       for index, filename in enumerate(files): # FIX
         f = os.path.join(filename)
         a.write(str(f) + os.linesep)

See the official documentation.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
  • It does create a column for indexes but there all equal to 0! `0 Sat_t0.txt` `0 Sat_t1.txt` `0 Sat_t2.txt` `0 Sat_t3.txt` – Joëlle Jul 13 '21 at 17:45
  • @Joelle, `enumerate` was applied in the wrong loop. Sorry about that. It should be working as you want now. – lmiguelvargasf Jul 13 '21 at 18:00
0

You can simply use a counter variable. The code might look like this:

import os

with open("bionaplsatfiles.dat", "w") as a:
    count = 0
    for path, subdirs, files in os.walk(r'D:\Bionapl_Nicolas\testKB\vtu_files\output_Sn'):
       for filename in files:
         f = os.path.join(filename)
         a.write(f'{count} {str(f)}{os.linesep}')
         count += 1
Triet Doan
  • 11,455
  • 8
  • 36
  • 69
  • this is copying my answer! – lmiguelvargasf Jul 13 '21 at 16:32
  • Sorry, but I didn't. I wrote my answer and didn't know that yours is already posted. – Triet Doan Jul 13 '21 at 16:36
  • will, it does create a column for indexes but it gives me 0 in all the indexes ! – Joëlle Jul 13 '21 at 16:50
  • Sorry, I realize that using `enumerate` just makes the solution complicated. Simply use a counter can solve the problem. Please check my updated answer. – Triet Doan Jul 13 '21 at 17:13
  • Perfect, it gives me what I need. One last question: do you have an idea how can I add the number of lines before the first line (in the same output that we just created)? – Joëlle Jul 13 '21 at 18:21
  • I can think of 2 options at the moment. 1st is: after creating the file, open it again and [prepend](https://stackoverflow.com/questions/5914627/prepend-line-to-beginning-of-a-file) the final `count` to the file. The 2nd option is: instead of writing directly to the file inside the loop, just store all file names in a list. Then, open the file, write the `len(my_list) + 1` (should be the total lines), then looping through the list with `enumerate` to print out index and file names. – Triet Doan Jul 13 '21 at 18:40