[英]how to make multiple folders in python at once for windows
Hey I am new to programming can anyone help point me in the right direction?嘿,我是编程新手,谁能帮我指出正确的方向? I am trying to make multiple folders at one for a sorting program pretty much checking if the folder exists and if it doesn't make one named after the file type its sorting.
我正在尝试为一个排序程序同时创建多个文件夹,以检查该文件夹是否存在,以及它是否没有以文件类型命名的文件夹对其进行排序。 I was looking it up and found some code but I don't understand where on the computer it is writing to.
我正在查找它并找到了一些代码,但我不明白它在计算机上的哪个位置写入。 Here is the code I found online:
这是我在网上找到的代码:
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)
Try this also也试试这个
import os
path = "parent"
for new in range(5):
print(f'{path}/child_{new}')
try:
os.makedirs(f'{path}/child_{new}')
except OSError:
print ("Creation of the directory %s failed" % path)
else:
print ("Successfully created the directory %s" % path)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.