Windows path in Python

后端 未结 5 1692
悲哀的现实
悲哀的现实 2020-11-21 04:18

What is the best way to represent a Windows directory, for example \"C:\\meshes\\as\"? I have been trying to modify a script but it never works because I can\'t

相关标签:
5条回答
  • 2020-11-21 04:55

    you can use always:

    'C:/mydir'
    

    this works both in linux and windows. Other posibility is

    'C:\\mydir'
    

    if you have problems with some names you can also try raw string literals:

    r'C:\mydir'
    

    however best practice is to use the os.path module functions that always select the correct configuration for your OS:

    os.path.join(mydir, myfile)
    

    From python 3.4 you can also use the pathlib module. This is equivelent to the above:

    pathlib.Path(mydir, myfile)
    

    or

    pathlib.Path(mydir) / myfile
    
    0 讨论(0)
  • 2020-11-21 04:59

    In case you'd like to paste windows path from other source (say, File Explorer) - you can do so via input() call in python console:

    >>> input()
    D:\EP\stuff\1111\this_is_a_long_path\you_dont_want\to_type\or_edit_by_hand
    'D:\\EP\\stuff\\1111\\this_is_a_long_path\\you_dont_want\\to_type\\or_edit_by_hand'
    

    Then just copy the result

    0 讨论(0)
  • 2020-11-21 05:10

    Use PowerShell

    In Windows, you can use / in your path just like Linux or macOS in all places as long as you use PowerShell as your command-line interface. It comes pre-installed on Windows and it supports many Linux commands like ls command.

    If you use Windows Command Prompt (the one that appears when you type cmd in Windows Start Menu), you need to specify paths with \ just inside it. You can use / paths in all other places (code editor, Python interactive mode, etc.).

    0 讨论(0)
  • 2020-11-21 05:12

    Use the os.path module.

    os.path.join( "C:", "meshes", "as" )
    

    Or use raw strings

    r"C:\meshes\as"
    

    I would also recommend no spaces in the path or file names. And you could use double backslashes in your strings.

    "C:\\meshes\\as.jpg"
    
    0 讨论(0)
  • 2020-11-21 05:16

    Yes, \ in Python string literals denotes the start of an escape sequence. In your path you have a valid two-character escape sequence \a, which is collapsed into one character that is ASCII Bell:

    >>> '\a'
    '\x07'
    >>> len('\a')
    1
    >>> 'C:\meshes\as'
    'C:\\meshes\x07s'
    >>> print('C:\meshes\as')
    C:\meshess
    

    Other common escape sequences include \t (tab), \n (line feed), \r (carriage return):

    >>> list('C:\test')
    ['C', ':', '\t', 'e', 's', 't']
    >>> list('C:\nest')
    ['C', ':', '\n', 'e', 's', 't']
    >>> list('C:\rest')
    ['C', ':', '\r', 'e', 's', 't']
    

    As you can see, in all these examples the backslash and the next character in the literal were grouped together to form a single character in the final string. The full list of Python's escape sequences is here.

    There are a variety of ways to deal with that:

    1. Python will not process escape sequences in string literals prefixed with r or R:

      >>> r'C:\meshes\as'
      'C:\\meshes\\as'
      >>> print(r'C:\meshes\as')
      C:\meshes\as
      
    2. Python on Windows should handle forward slashes, too.

    3. You could use os.path.join ...

      >>> import os
      >>> os.path.join('C:', os.sep, 'meshes', 'as')
      'C:\\meshes\\as'
      
    4. ... or the newer pathlib module

      >>> from pathlib import Path
      >>> Path('C:', '/', 'meshes', 'as')
      WindowsPath('C:/meshes/as')
      
    0 讨论(0)
提交回复
热议问题