I've always been sort of confused on the subject of directory traversal in Python, and have a situation I'm curious about: I have a file that I want to access in a directory essentially parallel to the one I'm currently in. Given this directory structure:
parentDirectory
subfldr1
-testfile.txt
subfldr2
-fileOpener.py
I'm trying to script in fileOpener.py to get out of subfldr2, get into subfldr1, and then call an open() on testfile.txt.
From browsing stackoverflow, I've seen people use os
and os.path
to accomplish this, but I've only found examples regarding files in subdirectories beneath the script's origin.
Working on this, I realized I could just relocate the script into subfldr1 and then all would be well, but my curiosity is piqued as to how this would be accomplished.
EDIT: This question pertains particularly to a Windows machine, as I don't know how drive letters and backslashes would factor into this.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…