我要到一個文件中分離到5個文件,第一行會part1的存儲,二線將存儲在第2部分如何在python中運行時創建文件句柄?
就是這個樣子
#1 #6 ... to part1
#2 #7 ... to part2
#3 #8 ... to part3
#4 #9 ... to part4
#5 #10 ... to part5
我需要創建文件1五個檔手柄到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()