Skip to content

Instantly share code, notes, and snippets.

@samimisami
Last active October 12, 2021 15:32
Show Gist options
  • Save samimisami/bebaa110bd89da6518e1cd2362e1a1ca to your computer and use it in GitHub Desktop.
Save samimisami/bebaa110bd89da6518e1cd2362e1a1ca to your computer and use it in GitHub Desktop.
File Name Changer in Python
import pathlib
import shutil
def rename_files():
path_data = pathlib.Path.cwd() / "z_ProjectFiles"
path_output = pathlib.Path.cwd() / "z_ProjectFiles" / "Renamed Files"
path_output.mkdir(parents=True, exist_ok=True)
for file in path_data.iterdir():
if file != path_output:
print(file)
shutil.copy(file, path_output)
for file in path_output.iterdir():
name_of_the_file = file.name
name_without_the_extension = name_of_the_file.replace('.txt', '')
new_file_name = name_without_the_extension + "_desktop" + file.suffix
file.rename(path_output / new_file_name)
if __name__ == '__main__':
rename_files()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment