扫码关注官方订阅号
假设我有1个类似这样方式生成的文件:
with open('my-file.txt','wb') as f: for x in xrange(300000): f.write('hello,world\n')
现在我们想按照每个文件1000行的方式对之前生成的文件进行拆分,该如何处理?
ringa_lee
with open('my-file.txt') as myfile: index = 0 while True: index += 1 try: with open('my-file-%03d.txt' % index, 'w') as f: for _ in xrange(1000): f.write(myfile.next()) except StopIteration: break
如果是不知文件有多大就用try来捕捉StopIteration异常来终止程序
with open('my-file.txt', 'r') as reader: counter = 0 findex = 0 for line in reader: if counter==0: writer = open('file-'+str(findex), 'w') print >> writer, line.strip() counter += 1 if counter >= 1000: writer.close() counter = 0 findex += 1
其實,Linux 的 split 指令就可以作到這件事了:
split
$ split -l 1000 my-file.txt
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
如果是不知文件有多大就用try来捕捉StopIteration异常来终止程序
其實,Linux 的
split指令就可以作到這件事了: