国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python分批定量讀取文件內容,輸出到不同文件中的方法

2020-02-16 00:01:13
字體:
來源:轉載
供稿:網友

一、文件內容的分發

應用場景:分批讀取共有358086行內容的txt文件,每取1000條輸出到一個文件當中

# coding=utf-8# 分批讀取共有358086行內容的txt文件,每取1000條輸出到一個文件當中txt_path = "E:/torrenthandle.txt"base_path="E:/torrent_distribution/"def distribution( ): f = open(txt_path,"r")  lines = f.readlines() f2=open(base_path+"1.txt","w") content="" for i in range( 1,len(lines) ):  if ( i%1000!=0 ):   content+=lines[i-1]  else:   content+=lines[i-1]   f2.write(content.strip('/n'))   block_path=base_path+str(i)+".txt"   f2=open(block_path,"w")   content="" #最后的掃尾工作 content+=lines[i]  f2.write(content.strip('/n'))  f2.close() f.close()distribution( )

二、文件夾(目錄)下的內容分發

應用場景:分批讀取目錄下的文件,每取1000條輸出到一個新的目錄當中

# coding: utf-8import osimport shutilsourcepath = "E://sample"distribution_path = "E://sample//distribution//" if __name__ =='__main__': rs = unicode(sourcepath , "utf8") count = 1 savepath = unicode(distribution_path+"1", "utf-8") if not os.path.exists(savepath):  os.makedirs(savepath) for rt,dirs,files in os.walk(rs):  for fname in files:   if ( count%1000!=0 ):    shutil.copy(rt + os.sep + fname,savepath)     #os.remove(rt + os.sep + fname)   else:    shutil.copy(rt + os.sep + fname,savepath)     #os.remove(rt + os.sep + fname)    savepath = unicode(distribution_path+str(count), "utf-8")    if not os.path.exists(savepath):     os.makedirs(savepath)   count+=1

以上這篇python分批定量讀取文件內容,輸出到不同文件中的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 奉节县| 高州市| 游戏| 墨竹工卡县| 陈巴尔虎旗| 潢川县| 报价| 阿合奇县| 广安市| 麦盖提县| 曲周县| 新河县| 墨竹工卡县| 甘泉县| 贵阳市| 钟山县| 孙吴县| 汾西县| 精河县| 菏泽市| 屯昌县| 黔西县| 海兴县| 吕梁市| 石家庄市| 陈巴尔虎旗| 黎川县| 吉安县| 隆安县| 开平市| 铜鼓县| 乌什县| 剑阁县| 颍上县| 沙河市| 张家口市| 文成县| 章丘市| 二连浩特市| 鱼台县| 敦煌市|