如何在文件资源管理器中自动创建文件夹?

0 投票
1 回答
55 浏览
提问于 2025-04-14 16:22

我想自动创建三个文件夹,分别叫做:CSV、image和PNG。我有一个'路径',文件就放在这个路径下。但是当我尝试用这个'路径'来创建文件夹时,出现了错误。我正在看一个YouTube视频来练习。

创建文件夹的代码:

folder_names = ['csv files', 'image files', 'text files']

for loop in range(0,3):
    if not os.path.exists(path = folder_names[loop]):
        print(folder_names[loop])
        os.makedirs(folder_names[loop])

错误信息:

TypeError Traceback (most recent call last) Cell In[83], line 7 5 for loop in range(0,3): 6 if not os.path.exists(path = folder_names[loop]): ----> 7 print(path = folder_names[loop]) 8 os.makedirs(path = folder_names[loop])

TypeError: 'path' is an invalid keyword argument for print()

1 个回答

-1

这个对我有效

import os

folder_names = ['a_dir', 'b_dir', 'c_dir']   # list of folders to create

for fn in folder_names:                      # run through the list
    if not os.path.exists(path = fn):        # check whether the folder alraedy exists
        print('create: ' ,fn)               
        os.makedirs(fn)                      # if not then create the folder
    else: print(fn, ' alraed exists')        # otherwise do nothing than to inform

撰写回答