3

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:\test\tool\folder_%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.

4

4 回答 4

5
import os, sys

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

    newpath = ((r'E:\test\tool\folder_%s') % (idx)) 
    if not os.path.exists(newpath): os.makedirs(newpath)

Try that, if it helps, accept the answer, if not leave a comment and I'll delete it.

于 2013-04-24T06:58:20.593 回答
1
newpath = r'E:\test\tool\folder_%s' % (idx) 
于 2013-04-24T06:58:18.393 回答
1

I think the preferred way to make strings is to use the format method.

newpath = 'E:\test\tool\folder_{0}'.format(idx)
于 2013-04-24T07:01:48.970 回答
1

This code will create the folder with name client_1001-test to client_1500-test

import os, sys

for i in range(1001, 1500):
    newpath = ((r'/tmp/fileSet/client_%s-test') % (i)) 
    if not os.path.exists(newpath): os.makedirs(newpath)
于 2014-03-12T04:46:48.743 回答