How to use "/" (directory separator) in both Linux and Windows in Python?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Track title: CC B Schuberts Piano Sonata No 16 D
--
Chapters
00:00 Question
00:45 Accepted answer (Score 321)
01:10 Answer 2 (Score 154)
01:28 Answer 3 (Score 88)
01:40 Answer 4 (Score 67)
02:14 Thank you
--
Full question
https://stackoverflow.com/questions/1601...
Answer 2 links:
[os.sep]: http://docs.python.org/3.3/library/os.ht...
Answer 3 links:
[os.path.normpath(pathname)]: https://docs.python.org/3/library/os.pat...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #linux #windows #unix
#avk47
--
Track title: CC B Schuberts Piano Sonata No 16 D
--
Chapters
00:00 Question
00:45 Accepted answer (Score 321)
01:10 Answer 2 (Score 154)
01:28 Answer 3 (Score 88)
01:40 Answer 4 (Score 67)
02:14 Thank you
--
Full question
https://stackoverflow.com/questions/1601...
Answer 2 links:
[os.sep]: http://docs.python.org/3.3/library/os.ht...
Answer 3 links:
[os.path.normpath(pathname)]: https://docs.python.org/3/library/os.pat...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #linux #windows #unix
#avk47
ACCEPTED ANSWER
Score 342
Use os.path.join().
Example: os.path.join(pathfile,"output","log.txt").
In your code that would be: rootTree.write(os.path.join(pathfile,"output","log.txt"))
ANSWER 2
Score 177
Use:
import os
print os.sep
to see how separator looks on a current OS.
In your code you can use:
import os
path = os.path.join('folder_name', 'file_name')
ANSWER 3
Score 98
You can use os.sep:
>>> import os
>>> os.sep
'/'
ANSWER 4
Score 73
os.path.normpath(pathname) should also be mentioned as it converts / path separators into \ separators on Windows. It also collapses redundant uplevel references... i.e., A/B and A/foo/../B and A/./B all become A/B. And if you are Windows, these all become A\B.