All you have to do is this:
import os
filename = 'video0.avi'
if os.path.exists(filename):
tmp = filename.split('.avi')
count = tmp[0].replace('video', '') or 0
count = int(count) + 1
new_filename = 'video{0}.avi'.format(count)
Basically, you check if it exists on disk, you will need the path to the file location, then split by the point and the file’s notation then remove the base name and increment the result, if it does not exist with "or 0" adds a defalt.
The same with function.
import os
def load_file(filename):
if os.path.exists(filename):
tmp = filename.split('.avi')
count = tmp[0].replace('video', '') or 0
count = int(count) + 1
return 'video{0}.avi'.format(count)
return filename
filename = 'video.avi'
print(load_file(filename))
All over the board.
import os, re, os.path
def load_file(file):
number = []
onlyfiles = [f for f in os.listdir('.') if os.path.isfile(os.path.join('.', f))] # load all dir files
filename, file_extension = os.path.splitext(file) # load filename and extension from parameter file
total_files = len(onlyfiles) # get total files in dir
splitter = re.compile(r'\d+') # build a splitter to get digits part of file name
number = splitter.findall(filename) or ['0'] # get number part from filename
only_name = filename.replace(number[0], '') # get only name from filename removing the number from it
i = 0
matching = [s for s in onlyfiles if only_name in s] # build array only with files names that match the only_name (from file name)
if len(matching) == 0: # if no files matchs the criteria returns the name itself, don't exist any
return file
while i < len(matching): # interates only the array of matching files and extracts the number from it
number.append((splitter.findall(matching[i]) or [0])[0]) # add to number array the numbers from file found
i += 1
number = [int(i) for i in number] # converts all number array to int (cast), some of then are strings
largest_number = max(number) # get the max value from array
return '{0}{1}{2}'.format(only_name, str(largest_number + 1), file_extension) # returns the new file number
filename = 'video.avi'
print(load_file(filename))
out = cv2.VideoWriter(load_file(filename), fourcc, fps, (int(width), int(height)))
and this in function form?? any idea? where the argument was the name of the output.
– FRango SStYle
That’s very easy, take a look now.
– Ernesto Casanova
oh yes sure xd, thank you very much!
– FRango SStYle
Okay, it works but it always picks up the filename video. After it runs again it picks up the video 0 again instead of video1b (which should generate Video2). basically wanted to take the ULTIMO and increase. some idea?? sorry there and thanks :c
– FRango SStYle
You need to go through the entire directory and check if there is a file with that name, this function is checking if the one you pass to it exists.
– Ernesto Casanova
and how I would walk the board?
– FRango SStYle
Take a look, I already put this function with search, confirms if it works. I have no way to test.
– Ernesto Casanova
Hmm thank you, I’ll test it now
– FRango SStYle
it will always fetch filename = 'video1.avi', creates Video2.avi, but then never creates video3.avi etcc, replaces Video2.avi
– FRango SStYle
I figured out why, I was validating with if out of while, it has to be while first, go through the entire directory looking for the file, if it finds goes to the maximum found for if and returns that maximum + 1. Now test.
– Ernesto Casanova
remains the same, nor do I understand why, the code seems.me to be correct, it replaces in the same the last generated video (Video2.avi)
– FRango SStYle
I mean, now not even trouble for Video2, keeps modifying video1, I was wrong on the last comment
– FRango SStYle
Lol...that basic error...I forgot the i += 1, it was always at zero.... he eh, try now
– Ernesto Casanova
just changing the video 1 :( I’m already without faith ahah :c, no longer understand anything xd
– FRango SStYle
Debug, try to figure out what’s missing there.
– Ernesto Casanova
yes that’s what I’m doing, thanks for the help!
– FRango SStYle
Hi, I decided. The solution was a little far away. Take a look now. I had to create a local project to test, yesterday I couldn’t. I left notes in the code so you can figure out what step by step is. It might not solve everything, but at least existing a video.avi file and other video10.avi, it will find the biggest and will return video11.avi..
– Ernesto Casanova
Thank you in advance! What you’re doing is taking the video with the highest number (e.g., video1), creating a Video2 (which n works) and creating a video3 (which is, what it’s supposed to be).. consecutively picks up the video 3, creates a video 4 that does not work and also creates a Video5 that is what is supposed to.. but this with a bit of debug should go to the place
– FRango SStYle
Now that I’ve got it tested, I don’t think you’re doing anything right. Look at the line I added to the code, that’s how you’re using it, with the base name, 'video.avi'...if you pass like this, it seems to me that it’s okay.
– Ernesto Casanova
the problem is that I call the function twice, but I already realized the error, I will already change my code. thanks for the help
– FRango SStYle