-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import os | ||
|
||
def change_extension_in_directory(directory, old_ext, new_ext): | ||
for root, dirs, files in os.walk(directory): | ||
for file in files: | ||
if file.endswith(old_ext): | ||
# 构造原始文件的完整路径 | ||
original_path = os.path.join(root, file) | ||
# 构造新的文件名 | ||
new_name = file[:-len(old_ext)] + new_ext | ||
new_path = os.path.join(root, new_name) | ||
# 重命名文件 | ||
os.rename(original_path, new_path) | ||
print(f'Renamed "{original_path}" to "{new_path}"') | ||
|
||
# 使用示例 | ||
current_directory = '.' # 当前目录 | ||
old_extension = '.java' | ||
new_extension = '.kt' | ||
change_extension_in_directory(current_directory, old_extension, new_extension) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters