mixed slashes with os.path.join on windows

前端 未结 7 1410
一生所求
一生所求 2020-11-27 04:39

I tend to use only forward slashes for paths (\'/\') and python is happy with it also on windows. In the description of os.path.join it says that is the correct way if you w

7条回答
  •  独厮守ぢ
    2020-11-27 05:01

    EDIT based on comment: path = os.path.normpath(path)

    My previous answer lacks the capability of handling escape characters and thus should not be used:

    • First, convert the path to an array of folders and file name.
    • Second, glue them back together using the correct symbol.

      import os   
      path = 'c:\www\app\my/folder/file.php'
      # split the path to parts by either slash symbol:
      path = re.compile(r"[\/]").split(path)
      # join the path using the correct slash symbol:
      path = os.path.join(*path)
      

提交回复
热议问题