如何写Python以读取名为“ file1.txt”的文本文件中的前两行。写下“ file1.txt”的两行读取。到新文件“ file2.txt”

发布于 2025-01-24 03:30:44 字数 33 浏览 0 评论 0原文

从名为“ file1.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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

紧拥背影 2025-01-31 03:30:44
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()

我敢肯定,那里的某个地方有一个整洁的解决方案,但这会起作用!
希望它能帮助您:)

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

请止步禁区 2025-01-31 03:30:44

编写一个python程序,以

  1. 读取名为“ file1.txt”的文本文件的前两

  2. “ file2.txt”
  3. 读取“ 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()

Write a Python program to

  1. Read the first two lines from a text file named "file1.txt"
  2. Write the two lines read from "file1.txt" to a new file called
    "file2.txt"
  3. 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()
带上头具痛哭 2025-01-31 03:30:44

对于2行:

with open("file1.txt", "r") as r:
    with open("file2.txt", "w") as w:
        w.write(r.readline() + r.readline())

每次调用r.readline(),它将转到下一行。因此,如果您想阅读n lines;使用:

请注意,如果在第一行的末尾有一条新行(\ n),则.Readline() + r.readline()仅是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())

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())
猫瑾少女 2025-01-31 03:30:44
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")
fcontent=f1.readline()
f2.write(fcontent)
fcontent=f1.readline()
f2.write(fcontent)
f1.close()
f2.close()
看海 2025-01-31 03:30:44
 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()
 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()
放低过去 2025-01-31 03:30:44
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")
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()
一个人的旅程 2025-01-31 03:30:44
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()
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()
违心° 2025-01-31 03:30:44
#reading the first 2 lines from the file named "file1.txt" and assigning the contents of 2 lines to variables named line1, line2
with open("file1.txt","r") as f1:
    line1=f1.readline()
    line2=f1.readline()
#openning the file named "file2.txt" to write those read lines(in write mode)
with open("file2.txt","w")as f2:
    f2.write(line1+line2)
#openning the same file in readable mode and print
with open("file2.txt","r")as readable:
    print(readable.read())
#with open() structure will close our files automatically
#for further studies refer this site https://www.w3schools.com/python/python_file_handling.asp
#reading the first 2 lines from the file named "file1.txt" and assigning the contents of 2 lines to variables named line1, line2
with open("file1.txt","r") as f1:
    line1=f1.readline()
    line2=f1.readline()
#openning the file named "file2.txt" to write those read lines(in write mode)
with open("file2.txt","w")as f2:
    f2.write(line1+line2)
#openning the same file in readable mode and print
with open("file2.txt","r")as readable:
    print(readable.read())
#with open() structure will close our files automatically
#for further studies refer this site https://www.w3schools.com/python/python_file_handling.asp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文