python - 如何在python中一次为windows创建多个文件夹
问题描述
嘿,我是编程新手,谁能帮我指出正确的方向?我正在尝试为一个排序程序同时创建多个文件夹,以检查该文件夹是否存在,以及它是否没有以文件类型命名的文件夹对其进行排序。我正在查找它并找到了一些代码,但我不明白它在计算机上的哪个位置写入。这是我在网上找到的代码:
import os
# define the name of the directory to be created
path = "/tmp/year/month/week/day"
try:
os.makedirs(path)
except OSError:
print ("Creation of the directory %s failed" % path)
else:
print ("Successfully created the directory %s" % path)
解决方案
To create multiple folders create a list of folder names and set the path to the parent-folder:
import os
# define the name of the directory to be created
path = r"D:\test"
lst_folders = [r"Folder_A", r"Folder_B", r"Folder_C"]
for new_folder in lst_folders:
print(new_folder)
path_new = os.path.join(path, new_folder)
try:
os.makedirs(path_new)
except OSError:
print ("Creation of the directory %s failed" % path)
else:
print ("Successfully created the directory %s" % path)
推荐阅读
- flutter - 如何在颤动中使用画布创建宠物爪标志
- sql - 如何将查询结果用作 liquibase defaultValueComputed?
- python - 你能在 Python 中预编译正则表达式,将它们保存到文件中,然后从文件中重新加载它们吗?
- python - Python - 解析 Junos 路由器配置以连续显示数据
- installation - 无法执行 /usr/local/bin/pip3:权限被拒绝
- javascript - 显示来自 firebase 存储的所有图像(反应原生)
- r - 绘制 3D 网络
- python - Conda-pack 破坏了 spyder
- python - 错误:只能将 str(不是“int”)连接到 str
- pandas - 在 groupBy 使用 pyspark 后保存数据框视图