简体   繁体   English

如何编写 python 从名为“file1.txt”的文本文件中读取前两行 将从“file1.txt”读取的两行写入新文件“file2.txt”

[英]How write python 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 "file2.txt"

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"从名为“file1.txt”的文本文件中读取前两行 将从“file1.txt”中读取的两行写入新文件“file2.txt”

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 :)希望它可以帮助你:)

Write a Python program to编写一个 Python 程序来

  1. Read the first two lines from a text file named "file1.txt"从名为“file1.txt”的文本文件中读取前两行
  2. Write the two lines read from "file1.txt" to a new file called "file2.txt"将从“file1.txt”读取的两行写入一个名为“file2.txt”的新文件
  3. Read "file2.txt" and Print the contents阅读“file2.txt”并打印内容
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()

For 2 lines:对于 2 行:

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.每次调用r.readline()时,它都会转到下一行。 So if you wanted to read n lines;因此,如果您想阅读n行; 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请注意, .readline() + r.readline()如果在第一行末尾有新行( \n ),则只有 2 行单独的行

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())
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()
 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()
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()
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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从名为“file1.txt”的文本文件中读取前两行 将从“file1.txt”中读取的两行写入新文件“file2.txt” - 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" Python 如何在一个 file.txt 中写入 5 行,在一个新的 file1.txt 中写入另外 3 行,例如 +=1 - Python How to write 5 lines in one file.txt and the other 3 lines in a new file1.txt with +=1 for example 从 txt 文件中一次读取两行 - Read two lines at a time from a txt file 将 file1.txt 中的每一行循环到 file2.txt 的所有行上进行比较 - Looping each row in file1.txt over all rows of file2.txt for comparing 如何依次读取两个txt文件并将其写入python中的新文件? - how to sequentially read two txt files and write into a new file in python? 从txt读取行。 文件,并将除第6行外的所有行写在另一行上,然后刷新第一个文件 - Read lines from a txt. file and write all except 6 first lines on another, then flush the first file FileNotFoundError:[Errno 2] 没有这样的文件或目录:'file1.txt' - FileNotFoundError: [Errno 2] No such file or directory: 'file1.txt' 使用 python 从 a.txt 文件中读取某些行 - Read in certain lines from a .txt file with python 读取/写入 Python 中的 txt 文件 - Read/Write to a txt file in Python 阅读后从txt文件中删除行 - Delete lines from a txt file after read
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM