Read the first two lines from a text file named "file1.txt" Write the two lines read from "file1.txt" to a new file "file2.txt"
Asked
Active
Viewed 5,292 times
7 Answers
1
a_file = open("file1.txt", "r")
number_of_lines = 2
with open("file2.txt", "w") as new_file:
for i in range(number_of_lines):
line = a_file.readline()
new_file.write(line)
a_file.close()
I'm sure there is a neater solution out there somewhere but this will work! Hope it helps you :)

Oakzeh
- 35
- 8
1
Write a Python program to
- Read the first two lines from a text file named "file1.txt"
- Write the two lines read from "file1.txt" to a new file called "file2.txt"
- Read "file2.txt" and Print the contents
fhandle1 = open("file1.txt","r")
fhandle2 = open("file2.txt","w")
str = fhandle1.readline()
fhandle2.write(str)
str = fhandle1.readline()
fhandle2.write(str)
fhandle1.close()
fhandle2.close()
fhandle3 = open("file2.txt")
print(fhandle3.read())
fhandle3.close()

Aruni Weerasekara
- 21
- 2
0
For 2 lines:
with open("file1.txt", "r") as r:
with open("file2.txt", "w") as w:
w.write(r.readline() + r.readline())
Each time r.readline()
is called, it goes to the next line. So if you wanted to read n
lines; use:
Note that .readline() + r.readline()
is only 2 seperate lines if there is a new line (\n
) at the end of the first line
with open("file1.txt", "r") as r:
with open("file2.txt", "w") as w:
# Change 2 to number of lines to read
for i in range(2):
w.write(r.readline())

Freddy Mcloughlan
- 4,129
- 1
- 13
- 29
0
f1=open("file1.txt","r")
f2=open("file2.txt","w")
fcontent=f1.readline()
f2.write(fcontent)
fcontent=f1.readline()
f2.write(fcontent)
f1.close()
f2.close()

PuffCake
- 1
- 2
0
f1 = open("file1.txt","r")
f2 = open("file2.txt","w")
str = f1.readline()
f2.write(str)
str = f1.readline()
f2.write(str)
f1.close()
f2.close()
f3 = open("file2.txt")
print(f3.read())
f3.close()

Forsep wicki
- 1
- 1
-
1Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). β Community Jun 13 '22 at 05:33
0
fhandle1 = open("file1.txt")
fhandle2 = open("file2.txt","w")
fcontents = fhandle1.readline()
fhandle2.write(fcontents)
fcontents = fhandle1.readline()
fhandle2.write(fcontents)
fhandle1.close()
fhandle2.close()
fhandle3 = open("file2.txt")
print(fhandle3.read())
fhandle3.close()

Umezhh
- 1
- 2
-
1Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). β Community Jul 14 '22 at 06:13
0
fhandle1 = open("file1.txt","r")
l1 = fhandle1.readline()
l2 = fhandle1.readline()
fhandle2 = open("file2.txt","w")
fhandle2.write(l1)
fhandle2.write(l2)
fhandle2 = open("file2.txt")
print(fhandle2.read())
fhandle2.close()

KD008
- 1
- 3
-
1As itβs currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). β Community Aug 13 '22 at 05:28