import os from sys import argv path = argv[1] # take the directory from the command line. for root, dirs, files in os.walk(path): for file in files: dir = os.path.split(root)[-1] os.rename(file, f"{dir}-{file}")os.walk starts from the 'path' and for each directory from the three returns a tuple:
{the_current_directory, a_list_of_all_directories_in_the_current_directory, a_list_of_all_files_in_the_current_directory)So, you just pass the path to the os.walk and for each iteration, you change the names of the 'files' from the tuple (root, dirs, files). 'files' is a list of all files in 'root', which is the current directory during the scan.