Skip to content
Advertisement

Python pathlib.Path library always cuts last folder from the path and adds it to the name of created file

So here is the problem, I have some path which is like this:

JavaScript

Then I use this to “make” it win path:

JavaScript

So now every time I use it, to create some other files inside of path directory, my files are created inside of “category” folder with prefix UFF, so file names are:

JavaScript

instead of

JavaScript

For creation of files I use:

JavaScript

Anyone has idea what is going on?

Advertisement

Answer

The last slash in get_path is dropped when passing to Path. The string you are passing to tree.write is exactly what you are seeing: 'C:/Users/user1/Desktop/Files/File/1/category/UFFsomefilename.XML'

You can fix this as followed:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement