-2

I have a small script, and I am trying to parse data from text file to Excel file instead of going till the counter which is till 166 it stops at 134 and then doesn't do anything. I have a file close operation also but it doesn't close the file and looks like the script continues to run. Any thoughts? What am I doing wrong ?

path = ('C:\\Users\\40081\\PycharmProjects\\abcd')
#file_name = open('parsed_DUT1.txt', 'r')
file_name = 'parsed_DUT1.txt'
count=1


for line in file_name:
    inputfile = open(file_name)
    outputfile = open("parsed_DUT1" + '.xls', 'w+')
    while count < 166:
        for line in inputfile:
            text = "TestNum_" + str(count*1)
            if text in line :
                #data = text[text.find(" ")+1:].split()[0]
                outputfile.writelines(line)
                count = count+1
    inputfile.close()
    outputfile.close()
Korn1699
  • 77
  • 1
  • 8
aps_s
  • 113
  • 4
  • 12

1 Answers1

1

w+

Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

You are opening the output file in w+ mode, that overwrites it everytime. Try with

outputfile = open("parsed_DUT1" + '.xls', 'a') # 'a' opens a file for appending.

I also suggest you to deal with files with with statement:

with open(file_name) as inputfile, open("parsed_DUT1" + '.xls', 'a') as outputfile:
    # do stuff with input and output files