Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- def remove_after_space(directory):
- for filename in os.listdir(directory):
- if ' ' in filename:
- new_filename = filename.split()[0]
- old_path = os.path.join(directory, filename)
- new_path = os.path.join(directory, new_filename)
- if os.path.exists(new_path):
- # Rename the existing file with a different name
- new_path = os.path.join(directory, new_filename + "_renamed")
- os.rename(old_path, new_path)
- print(f"Renamed: {filename} to {new_filename}_renamed")
- else:
- os.rename(old_path, new_path)
- print(f"Renamed: {filename} to {new_filename}")
- # Get the current working directory
- current_directory = os.getcwd()
- # Call the function to remove anything after a space in filenames
- remove_after_space(current_directory)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement