Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8586759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:23:24+00:00 2026-06-11T22:23:24+00:00

I have written a code which opens multiple files in a directory and prints

  • 0

I have written a code which opens multiple files in a directory and prints only the first instance of match of required text from each file as output.

Now I want this output in a file. Doing it simply by putting print >> file.txt,... or .write or csv.write inside loop will not serve the purpose.

My code is:

import re, os, csv, sys

path = "D:\\"
in_files = os.listdir(path)
moldesc = ['Match1', 'Match2']

for f in in_files:
    file = os.path.join(path, f)
    text = open(file, "r")
    for line in text:
        if moldesc[0] in line:
            Text1 = line.split()[-1]          
        if moldesc[1] in line:
            Text2 = line.split()[-1]
            print f, Text1, Text2               # I WANT THIS OUTPUT IN A FILE
            break
    text.close()

print "We are extraction done !!!"  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:23:26+00:00Added an answer on June 11, 2026 at 10:23 pm

    you managed to open a file for reading, that’s just one step away from opening a file for writing.

    out = open(outfile, "w") 
    for f in in_files:
        ...
        output_string = "{},{},{}\n".format(f, HOMO, LUMO)
        out.write(output_string)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some code in JSP to download .docx files which is hosted
I have written a piece of code which uses the PdfSharp library. The instance
I have a zip file which on first 'level' contains only directory. Something like:
I have written a code in which i want to open a HTM file
I have written PHP code which connects to MYSQL to look up information, sends
I have written a code which does the thing as the title says. The
Need assistance with the pexpect module I have written a simple code which would
I have written two pieces of code which I intended to have identical outputs,
I have written to code to read and write to registry which is working
I have written a custom server control which (pseudo-code) looks like public class MyCustomCtrl

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.