0

I want a list of directories called sim1, sim 2, etc. created when a particular variable in my for loop is equal to 1, 2, etc.

I know that I have to use the os package (specifically the os.makedirs() function for directory creation) but have unfortunately not found the documentation pertaining specifically to this.

for n in np.arange(0,100):
    os.makedirs('C:\\Users\\adeet\\Desktop\\'/n)

I tried the above code- got the error - unsupported operand type(s) for /: 'str' and 'float'

and when I tried this-

for n in np.arange(0,100):
    os.makedirs('C:\\Users\\adeet\\Desktop\\n')

It made a folder named 'n' once and then gave me an error -[WinError 183] Cannot create a file when that file already exists: 'C:\Users\adeet\Desktop\n'

AVT
  • 119
  • 1
  • 1
  • 9
  • Have you implemented something? It would be easy to answer for people if you post the work done by you. – Sumit S Chawla Jun 09 '18 at 16:28
  • Check this post for directory operations: https://stackoverflow.com/questions/273192/how-can-i-create-a-directory-if-it-does-not-exist – Sumit S Chawla Jun 09 '18 at 16:33
  • Edited to include my code – AVT Jun 09 '18 at 16:36
  • @Sam Saw that post before I posted the question here.. It doesn't answer my specific query – AVT Jun 09 '18 at 16:37
  • try os.makedirs('C:\\Users\\adeet\\Desktop\\'+str(n)) – Ben.T Jun 09 '18 at 16:47
  • 1
    @Ben.T worked like a charm – AVT Jun 09 '18 at 16:48
  • @Ben.T any solution for the csv- I have population_dfs[n].to_csv('C:\\Users\\adeet\\Desktop\\Epidemic Modelling\\Simulations\\Only Epidemic\\' +str(n)) – AVT Jun 09 '18 at 16:56
  • 1
    @AVT not sure I understand what you ask, so maybe ask a new question (or edit this one as you don't have answer yet) but what I can see is that you don't have a file name in your `to_csv` so try something like `...Only Epidemic\\' +str(n) +'\\file_name.csv')` – Ben.T Jun 09 '18 at 17:11

0 Answers0