开发者

create multiple folders in python automatically [duplicate]

开发者 https://www.devze.com 2023-01-05 20:39 出处:网络
This question already has answers here: mkdir -p functionality in Python [duplicate] (12 answers) Cl开发者_JAVA技巧osed 6 years ago.
This question already has answers here: mkdir -p functionality in Python [duplicate] (12 answers) Cl开发者_JAVA技巧osed 6 years ago.

I want to create a path like C:\sample\sample1\hello.py. It should automatically create the complete path from sample to hello.py, and all the directories in between. Is this possible in Python?


The following functions may help:

  • os.makedirs
  • os.path.dirname
  • os.path.basename


import os
root_path = 'C:\path'

folders = ['folder 01', 'folder 02', 'folder 03']

for folder in folders:

    os.mkdir(os.path.join(root_path, folder))
0

精彩评论

暂无评论...
验证码 换一张
取 消