【发布时间】:2014-03-25 14:36:23
【问题描述】:
我必须将一个 file.txt 分成更多的文件。代码如下:
a = 0
b = open("sorgente.txt", "r")
c = 5
d = 16 // c
e = 1
f = open("out"+str(e)+".txt", "w")
for line in b:
a += 1
f.writelines(line)
if a == d:
e += 1
a = 0
f.close()
f.close()
所以,如果我运行它,它会给我这个错误:
todoController\WordlistSplitter.py", line 9, in <module>
f.writelines(line)
ValueError: I/O operation on closed file
我知道如果您执行 for 循环,文件会关闭,因此我尝试将 f 放入 for 循环,但它不起作用,因为没有得到:
out1.txt
1
2
3
4
out2.txt
5
6
7
8
我只得到文件的最后一行。我该怎么办?有什么办法可以回忆起我之前定义的 open 函数?
【问题讨论】: