Create new folders using python

I am trying to create folders using the following code. Something is not correct and leads to error:

"TypeError: 'str' object is not callable"

import os, sys

a= [4,3,2,1]
print len(a)
for idx in range (len(a)):

    newpath = r'E:testtoolfolder_%s'(idx) 
    if not os.path.exists(newpath): os.makedirs(newpath)

Using os.makedirs I can create folders. However, I am not able to suffix those folders in a loop. Any ideas can be helpful. Thanks.


import os, sys

a= [4,3,2,1]
print len(a)
for idx in range (len(a)):

    newpath = ((r'E:testtoolfolder_%s') % (idx)) 
    if not os.path.exists(newpath): os.makedirs(newpath)

尝试一下,如果有帮助,接受答案,如果不留下评论,我会删除它。


newpath = r'E:testtoolfolder_%s' % (idx) 

我认为制作字符串的首选方法是使用格式化方法。

newpath = 'E:testtoolfolder_{0}'.format(idx)
链接地址: http://www.djcxy.com/p/9276.html

上一篇: 我需要编写一个Python脚本来排序图片,我该怎么做?

下一篇: 使用python创建新文件夹