有時因為測試需要,需要建立多個有規律的檔案,比如 test_1.txt test_2.txt …… test_300.txt。我們可以通過乙個 python 指令碼來實現此功能。
此指令碼中用到了 os 模組的 mknod
方法。該方法用來在檔案系統中建立乙個節點(file, device special file or named pipe)。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
fileprefix = 'test_' #檔案字首
filesuffix = '.txt' #檔案字尾
filenum = 300 #檔案個數
for i in range(1, filenum):
filename = fileprefix + str(i) + filesuffix
os.mknod(filename)
linux 系統下也可以使用 touch 命令實現上述功能。
touch test_.txt
Python批量合併多個txt檔案
coding utf 8 os模組中包含很多操作檔案和目錄的函式 import os 獲取目標資料夾的路徑 meragefiledir os.getcwd meragefiles 獲取當前資料夾中的檔名稱列表 filenames os.listdir meragefiledir 開啟當前目錄下的re...
Python之批量建立檔案
批量建立檔案其實很簡單,只需要按照需要建立寫檔案 寫完關閉當前寫檔案 建立新的寫檔案 寫完關閉當前檔案 不斷迴圈即可,以下是乙個簡單例子,將大檔案big.txt按照每1000行分割成乙個個小檔案,具體做法如下 coding utf 8 index 0 count 0 f in open d.txt ...
python批量建立資料夾
有時需要建立一些同型別的資料夾儲存階段性的檔案,可以py指令碼實現批量建立資料夾 首先建立資料夾有兩個api 建立單層資料夾 import os path dir example ifnot os.path.exists path os.mkdir path 遞迴建立資料夾 import os pa...