Process the input files inidivually

Currently, i am processing the input file all together. i am expecting to process input file iniduvally and send email.

US_input1.csv
US_input2.csv
US_input3.csv
US_input4.csv
US_input5.csv
US_input6.csv
US_input7.csv
US_input8.csv
#writing the column name
seen = set()
with open('inputfile.txt') as filenames, open('colfile.txt', 'w') as mfile:
    for filename in filenames:
        csvFile = pandas.read_csv(filename.strip(), sep="|", nrows=1)
        # displaying the contents of the CSV file
        for col in csvFile.columns:
            COL= col.upper()
            if not search("", COL):
               h = hash(col)
               if h not in seen:
                 mfile.write(col.upper() + "\n")
                 seen.add(h)
mfile.close()

with open('colfile.txt', 'r') as file1:
    # Read the lines from the first text file
    lines5 = file1.readlines()
file1.close()

# Convert the lines from the first text file to a set
lines5_set = set(lines5)

# Open the second text file in read mode
with open('measlisttable.txt', 'r') as file2:
    # Read the lines from the second text file
    lines10 = file2.readlines()
file2.close()
# Convert the lines from the second text file to a set
lines10_set = set(lines10)

# Find the unique lines between the two text files
unique_lines = lines5_set.difference(lines10_set)

# Iterate over the unique lines and print them
with open('output.txt', 'w') as sfile:
    for line6 in unique_lines:
        sfile.write("%s" % line6)
sfile.close()

#send email to user

SERVER1 = "user@mail.com"
TO=["test@mail.com"]
CC=["test@mail.com"]
FROM="test@mail.com"
SUBJECT = "extra field"
TEXT=""
TEXT0=""
email_file='output.txt'
fp = open(email_file,"r")
TEXT_LIST = fp.readlines()
fp.close()
email_file1='input.txt'
fp1 = open(email_file1,"r")
TEXT_LIST1 = fp1.readlines()
fp1.close()

for line in TEXT_LIST1:
    TEXT0=TEXT0+"\n"+line

for line in TEXT_LIST:
    TEXT=TEXT+"\n"+line
footer_msg=" "

header0= """
Processed Files:

"""

header= """
Below list to be added:
"""
footer = """
"""
TEXT = header0 + TEXT0 + header + TEXT + footer + footer_msg

# Prepare actual message

message = """\
From: %s
To: %s
Cc: %s
Subject: %s


%s
""" % (FROM, ", ".join(TO),", ".join(CC), SUBJECT, TEXT)

print(message)

# Send the mail
'''
server = smtplib.SMTP(SERVER1)
server.sendmail(FROM, TO, message)
server.set_debuglevel(2)
server.quit()
'''
try:
    smtpObj = smtplib.SMTP(SERVER1)
    smtpObj.sendmail(FROM, TO, message)
    smtpObj.set_debuglevel(2)
    print("Successfully sent email")
except:
    print("Error: unable to send email")

To process each input file individually and send an email for each of them, you’ll need to modify the code to loop through the input files and execute the processing and email-sending steps within the loop. Here’s a modified version of your code:

pythonCopy code

import pandas
import smtplib
from re import search

input_files = [
    'US_input1.csv',
    'US_input2.csv',
    'US_input3.csv',
    'US_input4.csv',
    'US_input5.csv',
    'US_input6.csv',
    'US_input7.csv',
    'US_input8.csv'
]

SERVER1 = "user@mail.com"
TO = ["test@mail.com"]
CC = ["test@mail.com"]
FROM = "test@mail.com"
SUBJECT = "extra field"

for input_file in input_files:
    seen = set()
    csvFile = pandas.read_csv(input_file, sep="|", nrows=1)
    
    with open('colfile.txt', 'w') as mfile:
        for col in csvFile.columns:
            COL = col.upper()
            if not search("", COL):
                h = hash(col)
                if h not in seen:
                    mfile.write(col.upper() + "\n")
                    seen.add(h)
    
    with open('colfile.txt', 'r') as file1:
        lines5 = file1.readlines()
    
    lines5_set = set(lines5)
    
    with open('measlisttable.txt', 'r') as file2:
        lines10 = file2.readlines()
    
    lines10_set = set(lines10)
    unique_lines = lines5_set.difference(lines10_set)
    
    with open('output.txt', 'w') as sfile:
        for line6 in unique_lines:
            sfile.write("%s" % line6)
    
    TEXT0 = input_file
    TEXT = ""
    with open('output.txt', 'r') as fp:
        TEXT_LIST = fp.readlines()
    
    for line in TEXT_LIST:
        TEXT += "\n" + line
    
    footer_msg = " "
    header0 = f"Processed File: {TEXT0}\n\n"
    header = "Below list to be added:\n"
    footer = "\n"
    TEXT = header0 + header + TEXT + footer + footer_msg
    
    message = """\
From: %s
To: %s
Cc: %s
Subject: %s


%s
""" % (FROM, ", ".join(TO), ", ".join(CC), SUBJECT, TEXT)
    
    try:
        smtpObj = smtplib.SMTP(SERVER1)
        smtpObj.sendmail(FROM, TO, message)
        smtpObj.set_debuglevel(2)
        print("Successfully sent email for file:", input_file)
    except:
        print("Error: unable to send email for file:", input_file)

Can you let me know how to pirnt both name and age together in single field.

Code

mfile.write("%s %s\n" % (name, age))
Output :
Ram 
25
Arun 
25
Vignesh 
30
Expected output:
Ram 25
Arun 25
Vignesh 30

mfile.write("%s %s"  % (name, age))

I have tried this and work out