windows利用python快速批量创建文件夹
刚好有这个需求,所以简单记一下,以后也方便自己用
前提:
1.有python环境以及安装os模块
2.txt与py文件在同一目录下
使用
1.先创建一个folder_names.txt的文件,把要批量创建的名称放里面
例如:
注意不要有特殊符号,否则可能会报错
2.再创建一个.py文件,文件名为 files.py
。
import os
def create_folders_from_txt(txt_file):
if not os.path.exists(txt_file):
print(f"File '{txt_file}' not found.")
return
with open(txt_file, 'r', encoding='utf-8') as f:
folder_names = f.read().splitlines()
for folder_name in folder_names:
folder_path = os.path.join('.', folder_name)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
print(f"Created folder '{folder_name}'")
else:
print(f"Folder '{folder_name}' already exists")
if __name__ == "__main__":
txt_file = "folder_names.txt"
create_folders_from_txt(txt_file)
在文件路径下运行cmd,运行命令
python files.py