Change working directory in shell with a python script

前端 未结 5 1310
遇见更好的自我
遇见更好的自我 2020-12-06 01:16

I want to implement a userland command that will take one of its arguments (path) and change the directory to that dir. After the program completion I would like the shell t

5条回答
  •  一整个雨季
    2020-12-06 01:35

    I shall try to show how to set a Bash terminal's working directory to whatever path a Python program wants in a fairly easy way.

    Only Bash can set its working directory, so routines are needed for Python and Bash. The Python program has a routine defined as:

    fob=open(somefile,"w")
    fob.write(dd)
    fob.close()
    

    "Somefile" could for convenience be a RAM disk file. Bash "mount" would show tmpfs mounted somewhere like "/run/user/1000", so somefile might be "/run/user/1000/pythonwkdir". "dd" is the full directory path name desired.

    The Bash file would look like:

    #!/bin/bash
    #pysync ---Command ". pysync" will set bash dir to what Python recorded
    cd `cat /run/user/1000/pythonwkdr`
    

提交回复
热议问题