For file in directories, rename file to directoryname
Question: I have let's say 5 directories, let's call them dir1, dir2, dir3, dir4, dir5.

These are all in the current directory. Each of them contains 1 files called
title.mkv. I want to rename the files to the directory name they are in, ie
the file title.mkv in dir1, I want to rename to dir1.mkv.

I also want to then move the file to another folder. What python tools do I
need for this besides os and glob?


Answer: If you have the full filename and directory, to rename the files, you can use

    
    
    import os
    f_name = 'E:/temp/nuke.mkv'
    # Removes '/' at the end of string
    while f_name.endswith('/'):
        f_name = f_name[:-1]
    # Generates List Containing Directories and File Name
    f_name_split = f_name.split('/')
    f_path = ''
    # Iterates Through f_name_split, adding directories to new_f_path
    for i in range(len(f_name_split)-1):
        f_path += f_name_split[i] + '/'
    # Makes New Name Based On Folder Name
    new_name = f_name_split[-2] + '.mkv'
    # Gets The Old File Name
    f_name = f_name_split[-1]
    # Renames The File
    os.rename(f_path + f_name, f_path + new_name)
    

To go through all of the directories, you could do it recursively, have the
system output it to a file [windows: dir /s /b /a > file.txt], or use os.walk.
To move a file, you can use os.rename(source, destination)

