
I've read through the posts (and more) and I have something that's almost workable. The next step, str.zfill(4), turns those into "0001", "564645", or "0005".Thanks so much for these suggestions. In the lambda m.group() returns a string matching the pattern \d+. Object and must return a replacement string to be used. If it is a callable, it's passed the match replĬan be either a string or a callable if a string, backslash escapes Occurrences of the pattern in string by the replacement repl. Return the string obtained by replacing the leftmost non-overlapping

Signature: re.sub(pattern, repl, string, count=0, flags=0) This works because re.sub() can take a callable as the replacement argument. Renumber_files(raw_input("Insert folder path: ")) New_name = re.sub(r'\d+', lambda m: m.group().zfill(zeroes), filename, count=1) In this case adding leading zeroes to the first number found in the filename. re.sub() can take a replacement function. Your code can be simplified a lot by using regular expression substitution. Print "Renamed files: %s" % renamed_files Print "Unexpected error while renaming %s:%s"%(new, sys.exc_info()) Os.renames(current_file_n, new) #It doesn't return anything Renamed_files.append(new) # Add renamed file's name to a list I need an explanation of what I'm doing wrong as simple as possible, or is there another method that I can use that will give me the desired outcome?Īs martineau said your indentation is messed up. I've got errors like windows error: can't find file, and another error can't connect str and list? The desired outcome is the same as the beginning list, but collated with zeros,like this: Re_name = os.renames(current_file_n, new) Print list() # trying to make the str into a list. # new_name comes like this but seperate, not in a list? Why? New_name = original_name + add_zero + ext Original_name, number = mysplit(file_name_part) # 01 and fileĪdd_zero = number.rjust(4, "0") # add 0001

# a function to make a new name with needed 0000įile_name_part, ext = os.path.splitext(filename) # file01 and. Print "Files in folder: %s" % folder_path

I did test versions, combination of the little knowledge I have, but it gets me confused.įolder_path = os.listdir(raw_input("Insert folder path: ")) Maybe work for every file format, like jpg, png, txt, docx and so on (becuse I'm not sure what will be in the folder, this code might be used for image sequences.) Trying to create a simple code, to batch rename a folder in windows.Ĭhange every number, like "file02.txt", turn 02 into 0002
