I'm trying to find how to create sequential directories with specific padding. For a given number it should create directories:

def createDirectories(number=10, name='seq'):
    for i in range(1, number+1):
        os.mkdir(name+i)

But it should produce directories in following naming convention

seq0010
seq0020
seq0030
...
seq0100

How to format numbers in that way?

有帮助吗?

解决方案 2

You can use this expression to obtain the directory name seq0010 from the input 1:

dirName = 'seq%03d0' % 1

Replace 1 with i and you're set.

其他提示

>>> myNum = '5'
>>> print myNum.zfill(3)
>>> '005'

You can also use string formatting to create the entire folder name at once:

>>> 'seq{0:03d}0'.format(3)
'seq0030'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top