Search code examples
pythonpython-3.xtkinterpython-3.4python-os

missing slash in os.path.join result from tkinter filedialog


What's wrong with the way I'm joining my path here?

Everything but the first item in the list will be properly joined.

I'm grabbing a path from a filedialog in tkinter.

i.e filedialog.askdirectory()

Example Path:

PATH = "C:/MyUserName/Desktop/SomeDir"

What I'm doing:

os.path.join(*(PATH.split("/") + ["somefile.txt"]))

This will print out the following:

C:MyUserName/Desktop/SomeDir/somefile.txt

Why does it lose the first /?


Solution

  • I needed to convert my initial path using os.normpath I was getting a filedialog input from tkinter and then trying to use the above path style / code in the question to access / create files.

    Because of the bad join / seperators on windows it was causing errors.