如何使用python进行shell导航

如何使用python进行shell导航,python,linux,shell,navigation,popen,Python,Linux,Shell,Navigation,Popen,我想用python将其cd到另一个目录,我想在shell上执行这个命令,并在其运行中导航到该目录 command = 'cd ../../../' os.popen(command) 这会将我的shell导航到新目录 import os os.chdir('/path/to/dir') os.chdir的文档是。您的目标只是复制cd命令,还是更改脚本其余部分的工作目录?导入操作系统,sys def nav:path=os.getcwd item=path.split'/'name=s

我想用python将其cd到另一个目录,我想在shell上执行这个命令,并在其运行中导航到该目录

command = 'cd ../../../'  
os.popen(command)  
这会将我的shell导航到新目录

import os
os.chdir('/path/to/dir')

os.chdir的文档是。

您的目标只是复制cd命令,还是更改脚本其余部分的工作目录?导入操作系统,sys def nav:path=os.getcwd item=path.split'/'name=sys.argv[1]match=name slash=inc=-1 length=lenitem for i in item:inc+=1 if i!=匹配:继续其他:断开墨迹=0字符串=“/”而墨迹!=inc:ink+=1 string+=item[ink]+/print string os.chdirstring nav