我想将一个文件分成5个文件,第一行将存储在part1,第二行将存储在part2
是这样的
#1 #6 ... to part1
#2 #7 ... to part2
#3 #8 ... to part3
#4 #9 ... to part4
#5 #10 ... to part5
我需要为 file1 到 file5 创建五个文件句柄,但似乎我不能(part+i)
用作文件句柄,我不知道我应该转换为哪种类型?细绳?
如何解决这个问题?谢谢
这是我的代码
sys.argv[1] is source file
sys.argv[2] is the directory to store
sys.argv[3] is the number of subfile
import os
import sys
for i in range(1,sys.argv[3]+1):
(part+i) = open(os.path.join(sys.argv[2],"part"+i),"w")
source = open(sys.argv[1],"r")
for idx,line in enumerate(source.readlines(),1):
position = idx % int(sys.argv[3])
if position == 0:
(part+sys.argv[3]).write(line)
else:
(part+idx).write(line)
for i in range(1,sys.argv[3]+1):
(part+i).close()
source.close()