I have multiple files
in various folders that look like this:
cat - dog - bark.docx
I want to bulk rename the files so that the string before the first hyphen is moved to the end of the filename:
dog - bark - cat.docx
Here’s what I’ve got so far:
import os
path = input(‘Copy and paste the location of the files.’)
for filename in os.listdir(path):
filename_without_ext = os.path.splitext(filename)[0]
extension = os.path.splitext(filename)[1]
str_to_move = filename.split('-')[0]
new_filename = filename_without_ext.split('-')[1:] + ' - '+ str_to_move + extension
os.rename(os.path.join(path, filename), os.path.join(path, new_filename))
When I run the code it brings up the error message
‘TypeError: can only concatenate list (not "str") to list’. I’d be very grateful if someone could tell me what I’m doing wrong.
I’m new to coding and Python (as you can probably tell) so go easy on me.