How to use “/” (directory separator) in both Linux and Windows in Python?

前端 未结 9 1685
日久生厌
日久生厌 2020-11-28 19:43

I have written a code in python which uses / to make a particular file in a folder, if I want to use the code in windows it will not work, is there a way by which I can use

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 20:09

    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"))

提交回复
热议问题