python读文件与分割

Python09

python读文件与分割,第1张

f = open("foo.txt") # 返回一个文件对象

line = f.readline() # 调用文件的 readline()方法

line.split("\001")

while line:

print line, # 后面跟 ',' 将忽略换行符

#print(line, end = '')  # 在 Python 3 中使用

line = f.readline()

f.close()

按照你的分法,代码很简单了。

N = 0.618

lines = open('a.txt','r').readlines()

lines_for_b = int(len(lines) * N)

lines_for_c = len(lines) - lines_for_b # the variable is useless in fact

open('b.txt,'w').write(''.join(lines[:lines_for_a]))

open('c.txt', 'w').write(''.join(lines[lines_for_a:]))