[Python] How to get proper case path from tkinters filedialog.askdirectory

Discussão em 'Python' iniciado por Stack, Junho 23, 2025.

  1. Stack

    Stack Membro Participativo

    Hy there, im pretty new in python and facing a problem, i cant find the answere.

    When receiving a path from tkinters filedialog.askdirectory, the path is "normcase" but i need to receive it as proper case. I'm using the path to kill a process with psutil - identified by its cmdline (apparently this is case sensitive)

    Original path: "C:\MyProjects\Folder1"

    Original process commandline: "C:\MyProjects\Folder1\process.exe"

    psutils will find and kill the process on windows when cmdline is set exactly to: "C:\MyProjects\Folder1\process.exe" but not as "c:\myprojects\folder1\process.exe"

    How can i get the original case?

    How i get the path with askdirectory:

    tk_path = filedialog.askdirectory(initialdir=r"C:\MyProjects", title="...")


    result: "c:\myprojects\folder1"

    I've tried to adjust the path with win32api:

    cased_path = win32api.GetLongPathName(tk_path)
    cased_path = os.path.splitext(cased_path)[0]


    result: "c:\MyFolder\Folder1" the "c:" is still lowercase

    and I've tried to adjust it with glob:

    cased_path = glob.glob(re.sub(r'([^:])(?=[/\\]|$)', r'[\1]', tk_path))[0]


    same result: "c:\MyProjects\Folder1" the "c:" is still lowercase

    My current workaround is to take the adjusted string (returned from win32api), split it, capitalize the first part and put all back together.

    splitted_path = cased_path.split(":",1)
    first_part = splitted_path[0].capitalize()
    second_part = ":" + splitted_path[1]
    final_path = first_part + second_part


    result: "C:\MyProjects\Folder1" This works, but feels really stupid ^^

    How i kill the process by its cmdline:

    def kill_process(commandline):
    for proc in psutil.process_iter(['pid', 'cmdline']):
    try:
    cmdline_list = proc.info.get('cmdline')
    if not cmdline_list:
    continue
    cmdline_str = ' '.join(cmdline_list)
    if commandline in cmdline_str:
    proc.kill()
    except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
    continue

    Continue reading...

Compartilhe esta Página