import os
# 指定文件夹路径
folder_path = r'F:\Python\Py_Projects\text'
# 判断文件夹是否存在 如果不存在,则创建文件夹
if not os.path.exists(folder_path):
os.makedirs(folder_path)
# 查找当前文件夹下文件数,以确定新文件名
files = os.listdir(folder_path)
print('listdir '+str(files))
file_count = 0
# 遍历目录中的每个内容
for item in files:
file_count += 1
print('file_count '+str(file_count))
# # 新文件的名称和路径
new_file_name = f'exp{file_count + 1}'
new_file_path = os.path.join(folder_path, new_file_name)
print('folder_path: '+folder_path)
print('new_file_path;'+new_file_path )
# 创建新文件
os.makedirs(new_file_path)
在文件夹下创建文件,后缀以数字递增
于 2024-03-09 18:09:36 首次发布