Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new folders using python

Tags:

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:\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.

like image 937
user741592 Avatar asked Sep 18 '25 18:09

user741592


2 Answers

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.

like image 55
hd1 Avatar answered Sep 21 '25 08:09

hd1


newpath = r'E:\test\tool\folder_%s' % (idx) 
like image 30
Asterisk Avatar answered Sep 21 '25 09:09

Asterisk