有时因为测试需要,需要创建多个有规律的文件,比如 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_{1..300}.txt