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

前端 未结 9 1674
日久生厌
日久生厌 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:07

    If you are fortunate enough to be running Python 3.4+, you can use pathlib:

    from pathlib import Path
    
    path = Path(dir, subdir, filename)  # returns a path of the system's path flavour
    

    or, equivalently,

    path = Path(dir) / subdir / filename
    

提交回复
热议问题