-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename_files_like_in_linux.py
37 lines (28 loc) · 1.79 KB
/
rename_files_like_in_linux.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
###With this Python script, you can provide a Perl-like regular expression as a search pattern, a replacement pattern, and a list of files as arguments.
###Here's an example of a Python script to accomplish this:
# filename: rename_files.py
import os
import re
import sys
import glob
def rename_files(search_pattern, replacement_pattern, files):
compiled_pattern = re.compile(search_pattern)
for filepath in files:
base_name = os.path.basename(filepath)
new_name = re.sub(compiled_pattern, replacement_pattern, base_name)
if new_name != base_name:
new_path = os.path.join(os.path.dirname(filepath), new_name)
os.rename(filepath, new_path)
if __name__ == "__main__":
if len(sys.argv) < 4:
print(f'Usage: python {sys.argv[0]} [search_pattern] [replacement_pattern] [file_glob]...')
else:
files = []
for file_glob in sys.argv[3:]:
files.extend(glob.glob(file_glob))
rename_files(sys.argv[1], sys.argv[2], files)
print('File renaming operation completed.')
###In this script, you provide a Perl-like regular expression as the search pattern, a replacement pattern and one or more glob-style file paths as command line arguments. For example,
###if you saved the script as "rename_files.py" and you wanted to change all the files in the current directory that start with "oldname" to start with "newname" instead, you would run:
###python rename_files.py '^oldname' 'newname' './oldname*'
###Make sure to replace '^oldname', 'newname', and './oldname*' with your search pattern, replacement pattern, and file glob(s). This example assumes you are in the directory containing the files you wish to rename. If the files are in a different directory, you should provide the correct path in the file glob argument.