1 #coding:utf-8 2 3 import random 4 import time 5 """ 6 注意盘符小写 7 8 """ 9 f = open("d:\MyData\zhaohz4\Desktop\train.txt","r") #源文件 10 fw = open("d:\MyData\zhaohz4\Desktop\sample_10.txt","w") #待写文件 11 """ 12 如下亦可 13 f = open("d:/MyData/zhaohz4/Desktop/train.txt","r") 14 fw = open("d:/MyData/zhaohz4/Desktop/sample_10.txt","w") 15 """ 16 def test(): 17 start = time.clock() 18 raw_list = f.readlines() 19 random.shuffle(raw_list) 20 for i in range(50): #随机抽取数目 n 21 fw.writelines(raw_list[i]) 22 23 end = time.clock() 24 print("cost time is %f" %(end - start)) 25 26 if __name__ =="__main__": 27 test()