pythonshellcommand-linesubprocesspwd

running subprocess with combined cd and pwd


I run subprocess in jupyter to get the core path. I have to go one folder up and then call pwd

Running:

import subprocess
mypath=subprocess.run("(cd .. && pwd)")

leads to a "No such file or directory: '(cd .. && pwd)' error. I guess the cd calls a directory call.

Can you help me out?


Solution

  • Frame challenge: subprocess is the wrong tool for this job.

    import os.path
    
    mypath = os.path.abspath(os.path.dirname(os.getcwd()))
    

    ...is both faster and portable to non-UNIXy operating systems.