Python - Read a file and Write into another file


This Python program reads a file, searches for a specific keyword and writes the matched lines into another file.

#!/usr/bin/python

def main():
    myfile = open('file-textexample.txt')

    myoutput_file = open("file-read-write-output.txt", "w")

    for str_line in myfile:
        if str_line.find('Error') > -1:
            print(str_line)
            myoutput_file.write(str_line + "\n")  # write to another file            
        else:
            print(".")

    # close both files    myoutput_file.close()
    myfile.close()


if __name__ == "__main__": main()



almirsCorner.com

#python #programming #code #coding #software #softwaredeveloper #softwareengineering #programmer

No comments:

Post a Comment