If I have a path for a given file, say 'C:\Folder\OtherFolder\AnotherFolder\file.txt', is there a way in python I can get all the parent directories for file.txt up to 'OtherFolder'? The depth of the path is not constant, so I can’t just iterate back a certain number of times, but I always need to get up to 'OtherFolder' regardless of depth. I feel like this should be easier than I’m making it, but they say Thursday is the new Monday. 🙂
If I have a path for a given file, say ‘C:\Folder\OtherFolder\AnotherFolder\file.txt’ , is there
Share
If you are using Windows, try this:
You can then split it using
os.path.sep(in your case\):to get the individual directories.
On a Linux machine a similar call returns what you mean: