import os
d = '/home/vivek/t'
l = os.listdir(d)
for i in l:
p = os.path.join(d, i)
if os.path.isfile(p) and i == 'tmp.txt':
with open(p, 'r') as f:
for index, line in enumerate(f.readlines()):
if index % 3 == 0:
with open(os.path.join(d, 'store_1.txt'), 'a') as s1:
s1.write(line)
elif index % 3 == 1:
with open(os.path.join(d, 'store_2.txt'), 'a') as s2:
s2.write(line)
elif index % 3 == 2:
with open(os.path.join(d, 'store_3.txt'), 'a') as s3:
s3.write(line)
for index, line in enumerate(my_file.readlines()):
if (index + 1) % 3 == 1: # means lines 1, 4 ,7, 10..
# write line to file1
elif (index + 1) % 3 == 2: # means lines 2, 5 ,8..
# write line to file2
else: # means lines 3, 6, 9
# write line to file3
'd'是指向与程序相关的所有文件所在目录的绝对路径tmp.txt文件'是你的原始文件。在
使用
%
:相关问题 更多 >
编程相关推荐