【问题标题】:how to do shell navigation using python如何使用python进行shell导航
【发布时间】:2013-01-26 20:10:48
【问题描述】:

我想用 python cd 到另一个目录,我想在它运行的 shell 上执行这个命令以导航到该目录。

command = 'cd ../../../'  
os.popen(command)  

这应该将我的 shell 导航到新目录

【问题讨论】:

  • 您的目标仅仅是复制 cd 命令,还是更改脚本其余部分的工作目录?

标签: python linux shell navigation popen


【解决方案1】:
import os
os.chdir('/path/to/dir')

os.chdir 的文档是here

【讨论】:

  • import os, sys def nav(): path = os.getcwd() item = path.split('/') name = sys.argv[1] match = name slash="" inc =-1 长度 = len(item) for i in item: inc+=1 if i != match: continue else: break ink = 0 string='/' while ink != inc: ink+=1 string += item[ink ]+"/" 打印字符串 os.chdir(string) nav()
猜你喜欢
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 1970-01-01
相关资源
最近更新 更多