创建100多个文件夹并按顺序命名 [英] Creating 100+ folders and naming them sequentially

查看:1038
本文介绍了创建100多个文件夹并按顺序命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用os.makedirs()工具创建143个文件夹,但遇到了一些麻烦。我一次只能生成一个文件夹,我想一次创建所有文件夹。我希望通过IMG_00160将文件夹命名为IMG_0016。文件夹标题,但是我无法使用它。

  import os 

范围(0,143):
计数= 0016

os.makedirs( C:\\Users\joshuarb\Desktop\Organized_Images\IMG + count )

count = count + 1


解决方案

使用直接数字更容易且更具可读性(请注意,您要在 range()调用中使用161,因为不包括最后一个数字) :

  import os 

表示范围(16,161):
os .makedirs( C:\\Users\joshuarb\Desktop\Organized_Images\IMG _ {:04d}。format(count))

还要注意,我使用了宽度为4的零填充,因为您的命名约定对于IMG_0016和IMG_0016是不同的0,所以我选择限制为DOS通常的8个字符。根据需要进行调整。


I'm trying to create 143 folders using the os.makedirs() tool, but I'm having some trouble. I can only generate one folder at a time, and I'd like to make all of them at once. I would like the folders to be named IMG_0016 through IMG_00160.

I thought of trying a for loop with a counter, where each loop would add the new number to the end of the folder title, but I can't get it to work.

import os

for folders in range(0, 143):
    count = 0016

    os.makedirs("C:\\Users\joshuarb\Desktop\Organized_Images\IMG"+count)

    count = count+1

解决方案

It is easier and more readable to use the direct numbers (note that you want to use 161 in the range() call, since the last number is not included):

import os

for count in range (16, 161):
    os.makedirs("C:\\Users\joshuarb\Desktop\Organized_Images\IMG_{:04d}".format(count))

Also notice that I used a zero-padding with width 4, since your naming convention differs for IMG_0016 and IMG_00160 so I chose to limit to DOS' usual 8-character. Adjust according to your needs.

这篇关于创建100多个文件夹并按顺序命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆