Code: Select all
from google.colab import drive
drive.mount('/content/drive')
Code: Select all
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')
Code: Select all
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.
Dieses Verhalten ist verwirrend, da es darauf hindeutet, dass die beiden Pfade als austauschbar behandelt werden.
Wenn die Ordner „MyDrive“ und „Mein Laufwerk“ auf denselben Ordner verweisen, dann ist dies der Fall Verhalten wird erwartet. Aber wenn das der Fall ist, wie funktioniert es unter der Haube? Wenn sie nicht auf denselben Ordner verweisen, warum verhält es sich dann so?
Andere Benutzer haben Probleme mit „Mein Laufwerk“ und „ MyDrive“-Pfade in Colab, wie in diesem StackOverflow-Beitrag von 2019 erwähnt. Aber das passiert mir nicht.
Zu dieser Frage gab es einen Vorschlag, dem Raum zu entfliehen. Es stimmt mit dem Beispielnotizbuch von Google überein: Externe Daten: Lokale Dateien, Laufwerk, Tabellen und Cloud-Speicher.
Code: Select all
# 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
Code: Select all
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}
Code: Select all
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!
Code: Select all
with open(path_with_escaped_space, 'w') as f:
f.write('qux')
führt
Code: Select all
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
in ()
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'
Ich würde gerne verstehen, warum dies geschieht und ob dies ein erwartetes Verhalten oder ein Problem bei der Interaktion von Colab mit Google Drive ist. insbesondere den Ordner „Mein Laufwerk“.