Darshan Malu
Darshan Malu

Reputation: 63

Python: Making a folder with incrementing number depending on existing folders with same name

When someone runs the python script,I want to create a folder named ID + '1' and subsequently next time if someone runs the script again it should put next number i.e. ID2 for creating new folder inside same directory.

I tried making a program

import os

path = 'C:\\Users\\Documents\\Python\\Projects'

for i in range (1,11):
    os.chdir(path)
    Newfolder= 'ID'+ str(i)
    os.makedirs(Newfolder)

But this creates 10 folders at once but I want only one folder to be created when someone runs the script.

Upvotes: 0

Views: 2157

Answers (4)

ashraful16
ashraful16

Reputation: 2782

For every time script running to create a directory, you need a variable that must have to save. In your code, I think the loop is useless. I solved in this way for every time script running. Delete the generated .npy file from your directory, when you want to start from 1

import os
import numpy as np
path = 'define your directory'
f='Flag_variable.npy'
try:
    Flag_variable = np.load('Flag_variable.npy')
    Flag_variable = int(Flag_variable)
    Flag_variable =Flag_variable+ 1
    np.save(f,Flag_variable)
except FileNotFoundError:
    
    np.save(f, 1)
    Flag_variable=1
    
 
os.chdir(path)
Newfolder= 'ID'+ str(Flag_variable)
os.makedirs(path+Newfolder)
print('Total Folder created', Flag_variable)


for i in range (1,11):
    pass
#     os.chdir(path)
#     Newfolder= 'ID'+ str(i)
#     os.makedirs(path+Newfolder)

Upvotes: 1

Daisuke Akagawa
Daisuke Akagawa

Reputation: 484

How about this one. if statement checks whether path candidate is already exists or not. if path_cand is not exist, you can make directory and break for loop

import os

path = 'C:\\Users\\Documents\\Python\\Projects'

for i in range(1, 11):
    path_cand = path + f"\\ID{i}"
    if not os.path.exists(path_cand):
        os.mkdir(path_cand)
        break

Upvotes: 1

David YK Han
David YK Han

Reputation: 150

I think you need to understand what the 'for' loop does. It basically iterates the inside code as many times as you want or set to.

In your code, the for loop iterates 10 times. That is the reason you get 10 folders created when you run your script once.

In your case, you want to remove the for loop and create just one folder. But, the important thing is that you want to know the number of folder you previously created so that the number in your next folder name follows the previous one.

In order to do that, you can read all the folders in your directory and get the latest one's name and parse it so you get the latest number. Then, increase the number by one and create the next folder.

Upvotes: 1

Jeffrey Hiraki
Jeffrey Hiraki

Reputation: 173

What you can do is use OS to search what folders are within the current dir, then take the highest number ID and create a new folder with that ID + 1.

import os

all_folders = os.listdir(path)
all_folders.sort()
latest = all_folders[-1].replace('ID', '')
new = int(latests) + 1
os.makedirs('ID'+str(latest))

Upvotes: 1

Related Questions