text - Writing to txt in python -
i'm writing lines of numbers text file text file. numbers print while running good, when open output file nothing has been written it. can't figure out why.
min1=open(output1,"w") oh_reader = open(filename, 'r') countmin = 0 while countmin <=300000: line in oh_reader: #min1 if countmin <=60000: hrvalue= int(line) ibihr = line print(line) print(countmin) min1.write(ibihr) min1.write("\n") countmin = countmin + hrvalue min1.close()
you should use python's with
statement open files. handles closing , safer:
with open(filename, 'r') oh_reader:
if way program indented
min1=open(output1,"w") oh_reader = open(filename, 'r') countmin = 0 while countmin <=300000: line in oh_reader: # same having pass here #min1 if countmin <=60000: hrvalue= int(line) ibihr = line print(line) print(countmin) min1.write(ibihr) min1.write("\n") countmin = countmin + hrvalue min1.close()
the for
loop empty nothing executed. fix this:
min1=open(output1,"w") oh_reader = open(filename, 'r') countmin = 0 while countmin <=300000: line in oh_reader: #min1 if countmin <=60000: hrvalue= int(line) ibihr = line print(line) print(countmin) min1.write(ibihr) min1.write("\n") countmin = countmin + hrvalue min1.close()
or alternatively:
min1=open(output1,"w") oh_reader = open(filename, 'r') countmin = 0 line in oh_reader: #min1 if countmin <=60000: hrvalue= int(line) ibihr = line print(line) print(countmin) min1.write(ibihr) min1.write("\n") countmin += hrvalue # += operator equal = + b min1.close()
Comments
Post a Comment