Skip to main content
fix error
Source Link

Edit: Incorrect to use re.sub with os.rename, check Reinderien's Answer

To fix this just remove the os.chdir(folder), there is no point doing this

# os.chdir(folder)
...
pattern = 'IMG_|PANO_|VID_'
renamed_filename = re.sub(pattern, '', filename)
file_path = os.path.join(folder, filename)
if renamed_filename != filename:
    number_modified_files +=1
    renamed_file_path = os.path.join(folder, renamed_filename)
    os.rename(file_path, renamed_file_path)

Edit: Incorrect to use re.sub with os.rename, check Reinderien's Answer

Edit: Incorrect to use re.sub with os.rename

To fix this just remove the os.chdir(folder), there is no point doing this

# os.chdir(folder)
...
pattern = 'IMG_|PANO_|VID_'
renamed_filename = re.sub(pattern, '', filename)
file_path = os.path.join(folder, filename)
if renamed_filename != filename:
    number_modified_files +=1
    renamed_file_path = os.path.join(folder, renamed_filename)
    os.rename(file_path, renamed_file_path)
Store replace pattern in list
Source Link

Use regex to replace pattern in filename

The structure of these if else all similar, you can use re to simplify it.

if "IMG_" in filename:
    os.rename(filename, filename.replace('IMG_', ''))
    number_modified_files +=1
elif "PANO_" in filename:
    os.rename(filename, filename.replace('PANO_', ''))
    number_modified_files +=1
elif "VID_" in filename:
    os.rename(filename, filename.replace('VID_', ''))   
    number_modified_files +=1

So you are looking for IMG_, PANO_ and VID_ in filename and try to replace it delete this part.

Instead of using os.rename multiply times, we can use re.sub(pattern, repl, string, count=0, flags=0) to do this.

It will Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.

pattern = 'IMG_|PANO_|VID_'
renamed_filename = re.sub(pattern, '', filename)

The pattern meaning match one in three. I am not sure if your are familiar with regex, here is the doc.

And if the renamed_filename not equal filename it is modified, so whole part will be

pattern = 'IMG_|PANO_|VID_'
renamed_filename = re.sub(pattern, '', filename)
if renamed_filename != filename:
    number_modified_files +=1
    os.rename(filename, renamed_filename)

Edit: Incorrect to use re.sub with os.rename, check Reinderien's Answer

Regex side effect

But the regex code will work differ from your original code, as in your code, the replace end if it match in one pattern, but regex solution will try to replace all patterns in IMG_ PANO_ and VID_.

Store replace pattern in list

I suggest you use a list to store the patterns(IMG_ PANO_ and VID_)

if you wanna stop replace in the first match, use a loop to check one by one,

patterns = ["IMG_", "PANO_", "VID_"]
...
for pattern in patterns:
    if pattern in filename:
        os.rename(filename, filename.replace(pattern, ''))
        number_modified_files +=1

Or if you wanna replace all patterns, use regex

re.compile("|".join(patterns))

It is easy for only 3 patterns now, but will drive you crazy if there are 30.

The structure of these if else all similar, you can use re to simplify it.

if "IMG_" in filename:
    os.rename(filename, filename.replace('IMG_', ''))
    number_modified_files +=1
elif "PANO_" in filename:
    os.rename(filename, filename.replace('PANO_', ''))
    number_modified_files +=1
elif "VID_" in filename:
    os.rename(filename, filename.replace('VID_', ''))   
    number_modified_files +=1

So you are looking for IMG_, PANO_ and VID_ in filename and try to replace it delete this part.

Instead of using os.rename multiply times, we can use re.sub(pattern, repl, string, count=0, flags=0) to do this.

It will Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.

pattern = 'IMG_|PANO_|VID_'
renamed_filename = re.sub(pattern, '', filename)

The pattern meaning match one in three. I am not sure if your are familiar with regex, here is the doc.

And if the renamed_filename not equal filename it is modified, so whole part will be

pattern = 'IMG_|PANO_|VID_'
renamed_filename = re.sub(pattern, '', filename)
if renamed_filename != filename:
    number_modified_files +=1
    os.rename(filename, renamed_filename)

Use regex to replace pattern in filename

The structure of these if else all similar, you can use re to simplify it.

if "IMG_" in filename:
    os.rename(filename, filename.replace('IMG_', ''))
    number_modified_files +=1
elif "PANO_" in filename:
    os.rename(filename, filename.replace('PANO_', ''))
    number_modified_files +=1
elif "VID_" in filename:
    os.rename(filename, filename.replace('VID_', ''))   
    number_modified_files +=1

So you are looking for IMG_, PANO_ and VID_ in filename and try to replace it delete this part.

Instead of using os.rename multiply times, we can use re.sub(pattern, repl, string, count=0, flags=0) to do this.

It will Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.

pattern = 'IMG_|PANO_|VID_'
renamed_filename = re.sub(pattern, '', filename)

The pattern meaning match one in three. I am not sure if your are familiar with regex, here is the doc.

And if the renamed_filename not equal filename it is modified, so whole part will be

pattern = 'IMG_|PANO_|VID_'
renamed_filename = re.sub(pattern, '', filename)
if renamed_filename != filename:
    number_modified_files +=1
    os.rename(filename, renamed_filename)

Edit: Incorrect to use re.sub with os.rename, check Reinderien's Answer

Regex side effect

But the regex code will work differ from your original code, as in your code, the replace end if it match in one pattern, but regex solution will try to replace all patterns in IMG_ PANO_ and VID_.

Store replace pattern in list

I suggest you use a list to store the patterns(IMG_ PANO_ and VID_)

if you wanna stop replace in the first match, use a loop to check one by one,

patterns = ["IMG_", "PANO_", "VID_"]
...
for pattern in patterns:
    if pattern in filename:
        os.rename(filename, filename.replace(pattern, ''))
        number_modified_files +=1

Or if you wanna replace all patterns, use regex

re.compile("|".join(patterns))

It is easy for only 3 patterns now, but will drive you crazy if there are 30.

Source Link

The structure of these if else all similar, you can use re to simplify it.

if "IMG_" in filename:
    os.rename(filename, filename.replace('IMG_', ''))
    number_modified_files +=1
elif "PANO_" in filename:
    os.rename(filename, filename.replace('PANO_', ''))
    number_modified_files +=1
elif "VID_" in filename:
    os.rename(filename, filename.replace('VID_', ''))   
    number_modified_files +=1

So you are looking for IMG_, PANO_ and VID_ in filename and try to replace it delete this part.

Instead of using os.rename multiply times, we can use re.sub(pattern, repl, string, count=0, flags=0) to do this.

It will Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.

pattern = 'IMG_|PANO_|VID_'
renamed_filename = re.sub(pattern, '', filename)

The pattern meaning match one in three. I am not sure if your are familiar with regex, here is the doc.

And if the renamed_filename not equal filename it is modified, so whole part will be

pattern = 'IMG_|PANO_|VID_'
renamed_filename = re.sub(pattern, '', filename)
if renamed_filename != filename:
    number_modified_files +=1
    os.rename(filename, renamed_filename)