File mover based on document name I wrote a script that places documents to the folders they should be (based on my preferences of course).
It even creates the folder if it doesn't exists.
Here's how it works:

You run de script by clicking on it. (idk if I should let this run in the background constantly, I don't use it that much)
Checks the selected directory for files with a &quot;-&quot; in their name.
(Physics- Homework.docx)
It checks the targeted directory (where my folder hierarchy is meant to be) if there's a folder named after the file's name before the &quot;-&quot; part. (&quot;Physics- Homework.docx&quot; goes to a folder named &quot;Physics&quot;)
If there isn't a folder, it creates one. If there is a folder, it just moves the file inside

So Like I said in the first step, you have to click to run this, I would love to make it automatic, but I don't want it to run in the background just to move a file twice in a week or so. I would love to know if there's another cool way to achieve this automatic way of working.
My idea (to achieve what I said above) was to create a &quot;.bat&quot; file that runs Word and the script at the same time (this time script keeps running in the background once started), next to script's based functionality, it would also check for the apps running in the background and when Word is closed, it performs the file movement and closes itself. But I don't know if this is a great idea or if it can be done in much simpler way.
I want to know what I could've done better. Since the code is relatively short, I feel like I have done a great job, but I believe that there's always room for improvement.
Here's the code:
import shutil
import os

Dir = &quot;D:/OneDrive/Documents/&quot;  # Where are the files? Add &quot;/&quot; at the end of your directory.
TragetDir = &quot;D:/OneDrive/Documents/school/6a9/&quot;  # Where are the target folders? Add &quot;/&quot; at the end of your directory.

FileNames = glob.glob(Dir + &quot;*-*&quot;)  # scans the dir with the selected character (*[selected character]*), searches directly for the name now.

StartIndex = len(Dir)  # The chars of the Dir are useless
for x in range(len(FileNames)):  # Each file gets moved one by one
    Index = FileNames[x].find(&quot;-&quot;)  # I always create my files using: &quot;class + &quot;-&quot; + topic&quot; way
    SFolder = FileNames[x][StartIndex:Index]  # My folders are always named after the subject, so &quot;Math- exam 1.docx&quot; should be placed in a folder named &quot;Math&quot;

    try: #tries to make a target dir
        os.makedirs(TragetDir + SFolder)
    except: #if the directory already exists just skips this process
        pass
    
    shutil.move(FileNames[x], TragetDir + SFolder)  # moves to the target folder
A few changes I would make:

Follow PEP 8.
Use the modern, high-level pathlib instead of a combination of the os module, glob, and shutil.
Be careful when using a bare except:, or except Exception:, see https://stackoverflow.com/q/54948548.

import pathlib

docs_dir = pathlib.Path('')
target_dir = pathlib.Path('')

for file in docs_dir.glob('*-*'):  # Each file gets moved one by one
    subject_name, file_name = str(file.name).split('-', maxsplit=1)
    subject_dir = target_dir.joinpath(subject_name)
    
    subject_dir.mkdir(exist_ok=True)
    file.rename(subject_dir.joinpath(file_name))

As an aside, it may be worth considering using a folder just for unsorted school-related files. It would be so easy to add a non-schoolwork file which contains a '-' to your documents folder, and have it moved unintentionally by the program.
You wrote in a comment:

However I aim for that &quot;I saved the file and it directly moved to the targeted folder&quot; approach.

If that's the case, you should look into file system watchers/watchdogs.