按行N等分某个文件


# --*-- coding:utf-8 --*--
import random
import math


def fanhui():
    into = random.randint(1, 10)
    jiaodu = random.uniform(math.pi/6, math.pi/3)
    # print math.cos(jiaodu)
    if into * math.cos(jiaodu) * 10.0 * math.sqrt(3) < 149:
        return int(math.ceil(into * math.cos(jiaodu) * math.sqrt(3)))
    else:
        fanhui()


for i in xrange(100):
    # fanhui()
    print fanhui()


# --*-- coding;utf-8 --*--
import random
import time
import os
"""
"""
n = 20
fname = 'test_2505503569_2647000035.txt'

def getTimeStamp():
    """
    """
    return str(int(time.time() * 1000))

files = []
for i in range(1, n + 1):
    name = 'data' + os.sep + str(i) + '_' + getTimeStamp() + '.txt'
    f = open(name, 'w')
    files.append(f)

f = open(fname, 'r')
while True:
    line = f.readline()
    if not line:
        break
    line = line.strip()
    idx = random.randint(1, n) - 1
    files[idx].write(line + '\n')

for file in files:
    file.flush()
    file.close()



# --*-- coding:utf-8 --*--
import sys
fname = sys.argv[1]
file = open(fname)
lines = file.readlines()
print len(lines)
lines.sort()
for line in lines:
        print line.strip()
 

 

posted on 2014-12-16 14:09  雨渐渐  阅读(174)  评论(0编辑  收藏  举报

导航