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 !!!"  
6
  • When you write print >> output, "some text", output should be an open file (or other file-like object), not a filename. Commented Sep 26, 2012 at 8:54
  • You'll get a NameError if moldesc[1] in line is true and moldesc[0] in line isn't... (The first time at least - otherwise, it may be Text1 is set from a line that's completely got nothing to do with the line that Text2 is being set for... [eg - subtly incorrect results)) Also, it doesn't seem to matter what match you get, you're always taking the last word from the line? You might as well just have if any(m in line for m in ['Match1', 'Match2']): ... Commented Sep 26, 2012 at 9:07
  • @DzinX Yes agreed print >> output, should have open file named "output" in w mode but still in currant script it then writes only output for matching from only first file in directory whereas i want fist matching instance from all files in directory... Commented Sep 26, 2012 at 9:14
  • @Jon Clements Ya i am matching different words and getting last word from the line and trying to print these words....so program is working fine. But i want to take this output in a file. Which i am not able to do. Also remember i am using multiple files as input. Commented Sep 26, 2012 at 9:19
  • So - Text1 is always before Text2 and the output of the last words of lines 36 (text1) and 3900 (text2) [just making those up] is the expected output? Commented Sep 26, 2012 at 9:56

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

4 Comments

output_string = "{},{},{}\n".format(f, HOMO, LUMO) AttributeError: 'str' object has no attribute 'format'
wow, are you using python 2.5? you can use output_string = "%s,%s,%s" % (f, text1, text2)
should be, make sure you close the file, if your still having problems, post the code with the question.
that was fantastic worked as magic for me simply closing file worked...many thanks

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.