[Python] Why does Google Colab treat "My Drive" and "MyDrive" google drive path...

Discussão em 'Python' iniciado por Stack, Fevereiro 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am using Google Colab and mounted my Google Drive with the standard command:

    from google.colab import drive
    drive.mount('/content/drive')


    I tried writing to the following paths:

    path_with_space = '/content/drive/My Drive/foo.txt'
    path_without_space = '/content/drive/MyDrive/foo.txt'

    with open(path_with_space, 'w') as f:
    f.write('bar')
    with open(path_without_space, 'w') as f:
    f.write('baz')


    I wrote files to two different paths: /content/drive/My Drive/ (with a space) and /content/drive/MyDrive/ (without a space). Both paths appeared to work without errors, and os.path.exists() confirmed both files exist.

    if os.path.exists(path_with_space):
    print(f'File {path_with_space} exists.')
    else:
    print(f'File {path_with_space} does not exist.')
    if os.path.exists(path_without_space):
    print(f'File {path_without_space} exists.')
    else:
    print(f'File {path_without_space} does not exist.')

    # Output:
    # File /content/drive/My Drive/foo.txt exists.
    # File /content/drive/MyDrive/foo.txt exists.


    However, when I checked my Google Drive, only one foo.txt was actually saved, and its content was from the second write operation.

    This behavior is confusing because it suggests the two paths are treated as interchangeable.

    If the MyDrive and My Drive folder do refer to the same folder, then this behavior is expected. But if that’s the case, how does it work under the hood? If they don’t refer to the same folder, then why does it behave this way?


    Other users have encountered issues with "My Drive" and "MyDrive" paths in Colab, as noted in this 2019 StackOverflow post. But this doesn't happen to me.

    On that question, there was a suggestion to escape the space. It matches with google's example notebook, External data: Local Files, Drive, Sheets, and Cloud Storage.

    # Example from https://colab.research.google.com/notebooks/io.ipynb
    with open('/content/drive/My Drive/foo.txt', 'w') as f:
    f.write('Hello Google Drive!')
    !cat /content/drive/My\ Drive/foo.txt


    I also investigated how escaping spaces works by testing these variations with shell commands:

    path_with_escaped_space = '/content/drive/My\ Drive/foo.txt'
    wrapped_path_with_escaped_space = "'/content/drive/My\ Drive/foo.txt'"
    wrapped_path_with_space = "'/content/drive/My Drive/foo.txt'"
    print("\nEscaped space: ")
    !cat {path_with_escaped_space}
    print("\nWrapped path with escaped space: ")
    !cat {wrapped_path_with_escaped_space}
    print("\nWith space: ")
    !cat {path_with_space}
    print("\nWrapped path with space: ")
    !cat {wrapped_path_with_space}
    print("\nWithout space: ")
    !cat {path_without_space}


    I got:

    Escaped space:
    Hello Google Drive!
    Wrapped path with escaped space:
    cat: '/content/drive/My\ Drive/foo.txt': No such file or directory

    With space:
    cat: /content/drive/My: No such file or directory
    cat: Drive/foo.txt: No such file or directory

    Wrapped path with space:
    Hello Google Drive!
    Without space:
    Hello Google Drive!


    I also tried it on the open() method:

    with open(path_with_escaped_space, 'w') as f:
    f.write('qux')


    Which results in

    ---------------------------------------------------------------------------
    FileNotFoundError Traceback (most recent call last)
    <ipython-input-14-05333cd98156> in <cell line: 41>()
    39 get_ipython().system('cat {path_without_space}')
    40
    ---> 41 with open(path_with_escaped_space, 'w') as f:
    42 f.write('qux')

    FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/My\\ Drive/foo.txt'


    I noticed open() interprets the path differently than shell command. The backslash in the path was escaped again, leading to an invalid path. Could this be the cause?


    I would like to understand why this happens and whether this is expected behavior or an issue with how Colab interacts with Google Drive, specifically the My Drive folder.

    Continue reading...

Compartilhe esta Página