need help on python A file named data.txt contains an unknown number of lines, e
ID: 3908937 • Letter: N
Question
need help on python
A file named data.txt contains an unknown number of lines, each consisting of a single integer. Write some code that creates two files, dataplus.txt and dataminus.txt, and copies all the lines of data1.txt that have positive integers to dataplus.txt, and all the lines of data1.txt that have negative integers to dataminus.txt. Zeros are not copied anywhere.
I got wrong from
file1=open("data.txt",'r')
file2=open("dataplus.txt",'w')
file3=open("dataminus.txt",'w')
for line in file1:
if int(line)>0:
file2.write(str(line))
elif int(line)<0:
file3.write(str(line))
else:
pass
Explanation / Answer
file1 = open("data.txt", 'r') file2 = open("dataplus.txt", 'w') file3 = open("dataminus.txt", 'w') for line in file1: line = int(line.strip()) if line > 0: file2.write(str(line) + ' ') elif line < 0: file3.write(str(line) + ' ')Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.