简体   繁体   中英

os.chdir relative path error

import os,pdb
import sys
print os.path.dirname(os.path.realpath(__file__))
pdb.set_trace()
os.chdir('../bci_framework')

Output:

D:\baseline\BCI-Comparison-Framework\bci_framework
WindowsError: (2, 'The system cannot find the file specified', '../bci_framework')

Why? The folder is present!

The file is in the directory bci_framework , which is why printing out it's path shows you that directory, but you aren't. Wherever you are, it doesn't have bci_framework as a sibling.

Print out os.getcwd() to determine your current location. Any relative paths to chdir will be relative to that location, not to the location of the file.

For example, if you are in the stackoverflow directory below, and run python ./abhishek/bci_framework/my_file.py , your current directory is stackoverflow . Going up one directory will not contain the bci_framework directory.

stackoverflow/
    davidism/
    abhishek/
        bci_framework/
            my_file.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM