0

I have a Dataset, which has 5 folders, in which each folder has 100 .txt files. Below code you can see that I am looping through every file, and removing certain words from those files using my StopWords.txt file.

After I remove the words I am appending the output in one file(filteredtext.txt). But I want to have these output exactly as my Dataset (5 folders which has 100 .txt file).

This is my code.

import re
import os

#insert stopwords files
stopwordfile = open("StopWords.txt", encoding='utf-8')  

# Use this to read file content as a stream:  
readstopword = stopwordfile.read() 
stop_words = readstopword.split() 

#file path to dataset
for path, _, files in os.walk("sinhala-set1"):
    for file_name in files:
        filepath = os.path.join(path, file_name)
        
        print(f"Checking --> {filepath}")
               
        file1 = open(filepath, encoding='utf-8')  

        # Use this to read file content as a stream:  
        line = file1.read() 
        words = line.split() 
        
        for r in words:  
            if not r in stop_words:  
                appendFile = open('filteredtext.txt','a', encoding='utf-8')  
                appendFile.write(" "+r)  
                appendFile.close()
3
  • So.. do you want us to help you create a file from python? Commented Mar 19, 2021 at 15:45
  • Ok, you have given some context and some code, which is good. But what is your precise question? Or what is wrong with the current code? Commented Mar 19, 2021 at 15:54
  • So instead of opening 'filteredtext.txt' for writing every time, why not do something like os.path.join(path, 'filtered_' + file_name)? Have you tried anything like that? Commented Mar 19, 2021 at 15:57

1 Answer 1

1

You are appending the file because you are opening the same .txt file with appending mode appendFile = open('filteredtext.txt','a', encoding='utf-8') If you want a separate file for each loop, open a different file like this:

output_file = open('output_' + file_name), 'w', encoding='utf-8')

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

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.