my_list = ['', 'drive', 'test', filename] path = os.path.join(*my_list)
Result for path is drive/test/filename
The desired result for path would be /drive/test/filename (with the leading slash, because i do have a '', at the start of my_list
I ended up using os.path.sep.join(my_list), which produces /drive/test/filename with the leading slash as desired, but i was wondering if there is a better way to do this?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
For absolute paths, the initial / is not really a delimiter so much as the name of the root directory. You could do the following:
my_list = ['/', 'drive, 'test', filename] path = os.path.join(*my_list)
Whether altering how you generate my_list is cleaner than your current solution is, I suspect, a matter of opinion.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0