Python之批量建立檔案

2021-07-28 06:07:51 字數 523 閱讀 5128

批量建立檔案其實很簡單,只需要按照需要建立寫檔案、寫完關閉當前寫檔案、建立新的寫檔案、寫完關閉當前檔案、、、不斷迴圈即可,以下是乙個簡單例子,將大檔案big.txt按照每1000行分割成乙個個小檔案,具體做法如下:

# -*- coding: utf-8 -*-

index = 0

count = 0

f_in = open("%d.txt" % index, "w")

with open("big.txt", "r") as f_out:

for line in f_out:

count += 1

f_in.write(line)

# 讀滿1000行之後,行計數置零,小檔案序號加一,建立乙個新的檔案寫資訊

if count == 1000:

f_in.close()

count = 0

index += 1

f_in = open("%d.txt" % index, "w")

python批量建立資料夾

有時需要建立一些同型別的資料夾儲存階段性的檔案,可以py指令碼實現批量建立資料夾 首先建立資料夾有兩個api 建立單層資料夾 import os path dir example ifnot os.path.exists path os.mkdir path 遞迴建立資料夾 import os pa...

python批量建立資料夾

參考資料 python os.path.join 產生的斜槓在windows和linux下的不同表現和解決方法 python獲取指定目錄下所有檔名os.walk和os.listdir 使用os.listdir 獲取路徑下所有檔名 判斷是否是資料夾有多種判斷方式,我使用if in top files ...

批量建立資料夾

import os 匯入模組 path test 設定建立後資料夾存放的位置 for i in range 30 41 這裡建立10個資料夾 定義乙個變數判斷檔案是否存在,path指代路徑,str i 指代資料夾的名字 k 03d i i ists os.path.exists path str k...