mtkilic
mtkilic

Reputation: 1243

Open Multiple text files and call function

Below code works perfectly, where it opens one text files and function parse_messages gets as parameter

def parse_messages(hl7):
    hl7_msgs = hl7.split("MSH|")
    hl7_msgs = ["{}{}".format("MSH|", x) for x in hl7_msgs if x]
    for hl7_msg in hl7_msgs:
       #does something..

with open('sample.txt', 'r') as f:
    hl7 = f.read()
df = parse_messages(hl7)

But now I have multiple text files in directory. I want to do open each one then call from parse_messages function. Here is what I tried so far.

But this only read last text file, not all of them

import glob
data_directory = "C:/Users/.../"
hl7_file = glob.glob(data_directory + '*.txt')

for file in hl7_file:
    with open(file, 'r') as hl7:
    hl7 = f.read()
df = parse_messages(hl7)

Upvotes: 0

Views: 640

Answers (2)

Skycc
Skycc

Reputation: 3555

in your read file loop for file in hl7_file, you are overwrite hl7 at every iteration leaving only the last read store at hl7 You probably wanna concatenate all contents of the files together

hl7 = ''
for file in hl7_file:
    with open(file, 'r') as f:
        hl7 += f.read()

df = parse_messages(hl7) # process all concatenate contents together

or you can call parse_messages function inside the loop with df list store the results as below

df = []
for file in hl7_file:
    with open(file, 'r') as f:
        hl7 = f.read()
        df.append(parse_messages(hl7))
# df[0] holds the result for 1st file read, df[1] for 2nd file and so on

Upvotes: 1

PythonProgrammi
PythonProgrammi

Reputation: 23443

This should work, if I understood what you want to do

import os

all = []
files = [x for x in os.listdir() if x.endswith(".txt")]
for x in files:
    with open(x, encoding='utf-8','r') as fileobj:
        content = fileobj.read()
        all.append(parse_message(content))

Upvotes: 0

Related Questions