注意:别把路径更改为“C:/”,极度危险(O(∩_∩)O哈哈~)
import os
root = "C:/Users/Administrator/Desktop/内存测试/"
# 新建文本文件
def createTxt(roots):
for i in range(1, 10):
with open(roots + "/" + str(i) + "bdm.txt", "a+") as files:
# 往文本里面写入1000行本当迷博客地址
for j in range(1000):
files.write("本当迷博客:https://www.bdmcom.cn\n")
# 新建文件夹
def createFolder(roots):
n = 1
for i in range(10):
fileNames = "本当迷" + str(n) + "号"
data_path = os.path.join(roots, fileNames) # 文件夹路径roots
if not os.path.exists(data_path): # 判断文件夹是否存在
os.makedirs(data_path) # 不存在则新建文件夹
n = n + 1
# 获取path当前路径下的所有目录
def getAllDir(path, res):
createFolder(path)
createTxt(path)
# 得到当前目录下所有的文件
filesList = os.listdir(path)
# 处理每一个文件
for fileName in filesList:
# 判断是否是路径(用绝对路径)
fileAbsPath = os.path.join(path, fileName)
# 判断是否是文件夹
if os.path.isdir(fileAbsPath) and res >= 0:
createFolder(fileAbsPath)
createTxt(fileAbsPath)
# 递归调用
getAllDir(fileAbsPath, res - 1)
# else:
# print(sp + "普通文件:", fileName)
if __name__ == '__main__':
res = 2 # 递归层数(注意:层数太多可能会爆栈)
getAllDir(root, res)
print(os.listdir(root))