将来自 file.txt 的行传输到单独的文件中

Transfer lines from file.txt in separate files

我有这个文件 file.txt,其中包含 3 行:

hello
there
world

我想将 file.txt 中的每一行发送到单独的文件中,如下所示:

file0 一行

hello

file1 一行

there

file2 一行

world

这是我现在尝试过的方法,但不正确

cnt = 0
with open("file.txt", 'r') as f:
    x = f.readlines()
    with open("file" + str(cnt), "w") as g:
        for i in range(len(x)):
            single_line = x[i]
            g.write(single_line)
        cnt = cnt + 1

这只给我 1 个名为 file0 的文件,其中包含所有 3 行。

更新:

cnt = 0
with open("./to_split", 'r') as f:
    x = f.readlines()
    for i in range(len(x)):
        with open("file" + str(cnt), "w") as g:
            single_line = x[i]
            g.write(single_line)
        cnt = cnt + 1
cnt = 0
with open("./to_split", 'r') as f:
    x = f.readlines()
    for i in range(len(x)):
        with open("file" + str(cnt), "w") as g:
            single_line = x[i]
            g.write(single_line)
        cnt = cnt + 1