-1

I need to run around 6k .txt files through a python program but i am unable to loop it because it always gives an error message of file not found

I have tried

    for i in range(1,len(os.listdir("kepler_data_c03"))):
        file="file_"+(str(i))+".txt"

but it says file_1.txt not found although the kepler_data_co3 folder is in the same folder as the python program

alex.b
  • 1
  • Does this answer your question? [How to reliably open a file in the same directory as the currently running script](https://stackoverflow.com/questions/4060221/how-to-reliably-open-a-file-in-the-same-directory-as-the-currently-running-scrip) – mkrieger1 Mar 26 '23 at 22:17

1 Answers1

0

Either you need to check if the file already exists using os.path.isfile or just loop through all the file in that folder like this

import os

for file in os.listdir("kepler_data_c03"):
    if file.endswith('.txt'):
        your_file = os.path.join("kepler_data_c03", file)
        # do what you want to do with your_file
D.L
  • 4,339
  • 5
  • 22
  • 45
Sagun Shrestha
  • 1,188
  • 10
  • 23