0

I have 10 text files in the same folder. I want to process each text file individually in a for loop.

def qwerty():
    with open('file.txt') as f:
       dicts = {}
       for words in f:
          split_words = reg.finditer(words)
          for line in split_words:
             group1 = line.group(1)
             if group1 not in dicts:
                dicts[group1] = 1
             else:
                dicts[group1] += 1 
     return dicts

This is my code to process the individual text file. How do I run a loop that would process all the text files I have one by one? So the amount of text files correspond to the number of iterations in the for loop.

4
  • Do these text file names follow a pattern? You could from glob import glob and then for filename in glob("*.txt"). Commented Feb 26, 2017 at 18:46
  • @tdelaney Hi, no the text file don't follow a name pattern Commented Feb 26, 2017 at 18:49
  • Do you want to process all files in that directory? Commented Feb 26, 2017 at 18:52
  • sorry they are all .txt files @tdelaney Commented Feb 26, 2017 at 19:39

2 Answers 2

2

You may use os module to iterate over all the files in current directry and filter only the txt files as :

import os

for file_name in os.listdir("./"):
    if file_name.endswith(".txt"): # Add some other pattern if possible
        # Call your method here

Your current directory would contain some other files as well, which may not get filtered, So it would be a better idea to move the txt files to a separate location.

Sign up to request clarification or add additional context in comments.

5 Comments

Apparently, these files don't have a .txt extension. Using os.path.isfile may be a good change.
But in the given snippet the OP was using 'file.txt' which made me to assume that the text files have extension .txt
I made the same assumption in comments and was corrected. They don't follow a *.txt pattern. This will be the right answer... if we can get OP to tell us what rules to follow. Using isfile gets all files in the directory and that's the closest match to his description.
But isFile would be true for all files in current directory, It includes .py, .pyc, .blah, .foo, .bar and I don't guess it would do any good, However I have suggested to move the files to another directory and iterate over all the files in that directory.
But it would be false for subdirectories. OP has told us that "*.txt" doesn't work so you'll have to change something. All we know is there is a directory with files to process, so processing all of the files is apparently what he wants.
0

You can enumerate the files in a folder and use that in a for loop

import os
from glob import glob

def qwerty(folder_path="."):
    for filename in glob(os.path.join(folder_path, "*.txt")):
        ... do your processing

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.