Skip to content
Advertisement

Given a folder path in Colab, how can I extract a specific portion of that in python?

For an example, I have my folder path as “/content/data/a/b/c/d/” How can I extract the “b” from this path?

Advertisement

Answer

If “b” never moves

file_path = "/content/data/a/b/c/d/"
file_path.split('/')[4]

If it does move

file_path = "/content/data/a/b/c/d/"
file_path[file_path.index("b")]
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement