Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting the name of new created folder in the path of csv file

I am trying to create new csv file in the new created folder, i am asking how can i put the name of new created folder in the path of the csv file

   import os  
   def creat(i,ii):   
   # Directory  
   directory = "NEW"+str(i) 
   # Parent Directory path  
   parent_dir = 'C:\\Users\\lap\\Desktop\\parfolder\\'
   path = os.path.join(parent_dir, directory )   
   os.mkdir(path)  
   print("Directory '% s' created" % directory)  
   with open('C:\\Users\\lap\\Desktop\\parfolder\\%s\\MM%s.csv' %directory 
             %ii , 'w') as file:
        for i in range(1,10):
            file.write("{}\n".format(i))
for i in range(1,4):
   creat(i,i)
like image 849
lena Avatar asked Jan 20 '26 15:01

lena


1 Answers

Based on your error you can wrap the os.mkdir(path) call in a try, except construct:

try:
    os.mkdir(path)
except FileExistsError:
    pass

Edit:
Now you changed the code so you'll have to change this, too:

with open('C:\\Users\\lap\\Desktop\\parfolder\\%s\\MM%s.csv' % (directory,  
          i), 'w') as file:
like image 187
mechanical_meat Avatar answered Jan 22 '26 04:01

mechanical_meat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!