I’m learning python and have built a very simple program that just renames files. It makes all letters lowercase and replaces spaces and %20-instances with underscores and then copies them into a ‘Finished’ folder. I’m looking for better methods and approaches of writing and structuring my code.
Can anybody find any obvious imperfections or methods of improvement? If anyone has any advice then that would be great.
My code can be found below:
import os from shutil import copyfile files_to_improve_dir = 'Files to Improve' finished_files_dir = 'Finished' print('isdir', os.path.isdir(files_to_improve_dir)) # 1 - copy files into 'finished' folder for f in os.listdir(files_to_improve_dir): copyfile(os.path.join(files_to_improve_dir, f), os.path.join(finished_files_dir, f)) for f in os.listdir(finished_files_dir): new_filename = f.replace(' ', '_').replace('%20', '_').lower() print('f', f, 'new_filename', new_filename) if (f != new_filename): os.rename(os.path.join(finished_files_dir, f), os.path.join(finished_files_dir, new_filename))
Thanks for any advice here.