4

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
  • 3
    I think you forgot a % after the string before (idx). Commented Apr 24, 2013 at 6:57
  • Sorry. That was too dumb of me. Please disregard this question. The orignal code is big and I seem to have overlooked that % accidently got deleted Commented Apr 24, 2013 at 7:01
  • 3
    That's a pretty strange way to loop through 0,1,2,3. Commented Apr 24, 2013 at 7:02
  • Please see this stackoverflow.com/questions/273192/… since your code has a potential race condition Commented Apr 24, 2013 at 7:22

4 Answers 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.

Sign up to request clarification or add additional context in comments.

Comments

1
newpath = r'E:\test\tool\folder_%s' % (idx) 

Comments

1

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

newpath = 'E:\test\tool\folder_{0}'.format(idx)

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.