Advertisement
Najeebsk

PASTEBIN-REMOVE-ID-NAME.py

Mar 24th, 2024 (edited)
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. import os
  2.  
  3. def remove_after_space(directory):
  4.     for filename in os.listdir(directory):
  5.         if ' ' in filename:
  6.             new_filename = filename.split()[0]
  7.             old_path = os.path.join(directory, filename)
  8.             new_path = os.path.join(directory, new_filename)
  9.             if os.path.exists(new_path):
  10.                 # Rename the existing file with a different name
  11.                 new_path = os.path.join(directory, new_filename + "_renamed")
  12.                 os.rename(old_path, new_path)
  13.                 print(f"Renamed: {filename} to {new_filename}_renamed")
  14.             else:
  15.                 os.rename(old_path, new_path)
  16.                 print(f"Renamed: {filename} to {new_filename}")
  17.  
  18. # Get the current working directory
  19. current_directory = os.getcwd()
  20.  
  21. # Call the function to remove anything after a space in filenames
  22. remove_after_space(current_directory)
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement