Python Forum
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running multiple files
#1
So I've cobbled together a python script that, in essence, runs through a txt file and pulls out words or phrases that match a set of words/phrases that I've given it in a separate list. My question is whether or not there is a way to have python run all of the txt files (+- 700) without having to manually change the file name every time. In short, having the script run all of the txt files in a directory, hopefully in order or at least separating them so they don't all add together at the end. I've had some serious help getting this far, but I've been so far unlucky with this, and now that the project has grown exponentially (100 ---> 700 files) changing the file name manually is more and more of a pain.

File1 = open("A1.txt", "r") 
File2 = open("applicable_words.txt", "r")

Filewords = {}
badSymbols = (".", ",", "!", "?", "’", '”', '“',)
totalCount = 0

applicableWords = []
for line in File2:
    applicableWords.append((line.lower()).strip())
for line in File1:
    if not line.startswith("Q:"):
        for word in line.split( ):
            s = (word.lower())
            for symbol in badSymbols:
                s = s.replace(symbol, "")
            if len(s) > 1:
                if s in Filewords:
                    Filewords[s] += 1
                    totalCount += 1
                else:
                    Filewords[s] = 1
                    totalCount += 1
            else:
                totalCount += 1
                
frequencyList = []
for word in Filewords:
    if word in applicableWords:
        if len(frequencyList) == 0 or Filewords[word] == 1:
            frequencyList.append(word)
        else:
            for item in frequencyList:
                if not word in frequencyList and Filewords[word] >= Filewords[item]:
                    frequencyList.insert(frequencyList.index(item), word)

for value in frequencyList:
    print(value + ": " + str(Filewords[value]))
    
print()
print("Total number of words said: " + str(totalCount))

File1.close();
File2.close();
Reply
#2
So you have sepperate .txt files all sitting in the same folder and as of now you are importing them individually? If that is the case, here's something that might help:

For large files:

filenames = ['file1.txt', 'file2.txt', ...]
with open('path/to/output/file', 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            for line in infile:
                outfile.write(line)
For small files:

filenames = ['file1.txt', 'file2.txt', ...]
with open('path/to/output/file', 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            outfile.write(infile.read())
That way you can just combine all the text files into one larger text file.
Reply
#3
Thanks Vysero, I'll give that a try!
Reply
#4
(Jul-18-2018, 09:16 PM)Totalnoobwithhelp Wrote: Thanks Vysero, I'll give that a try!

NP Cool
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,535 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  splitting file into multiple files by searching for string AlphaInc 2 890 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Merging multiple csv files with same X,Y,Z in each Auz_Pete 3 1,152 Feb-21-2023, 04:21 AM
Last Post: Auz_Pete
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 957 Feb-15-2023, 05:34 PM
Last Post: zsousa
  Find duplicate files in multiple directories Pavel_47 9 3,096 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  Extract parts of multiple log-files and put it in a dataframe hasiro 4 2,085 Apr-27-2022, 12:44 PM
Last Post: hasiro
  Search multiple CSV files for a string or strings cubangt 7 8,006 Feb-23-2022, 12:53 AM
Last Post: Pedroski55
  Rename part of filename in multiple files atomxkai 7 7,328 Feb-18-2022, 10:03 PM
Last Post: atomxkai
  Process multiple pdf files Spartan314 1 1,316 Oct-27-2021, 10:46 PM
Last Post: Larz60+
  Generate Multiple sql Files With csv inputs vkomarag 13 4,204 Aug-20-2021, 07:03 PM
Last Post: vkomarag

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020