【发布时间】:2018-12-09 15:30:45
【问题描述】:
我想出了这个代码:
import os, subprocess, sys
location = os.path.dirname(os.path.realpath(__file__))
file = os.path.basename(__file__)
#print location # + r'\' + file
user_in = raw_input(location + '>')
if user_in == 'cd ..':
proc = subprocess.Popen('cd .. && cd', shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin= subprocess.PIPE)
new_location = proc.stdout.read() + proc.stderr.read() + '>'
#new_location = str(new_location) + '>'
new_location = new_location.replace(r'\r','')
new_location = new_location.replace(' ','')
print new_location
#new_user_in = raw_input(str(new_location) + '>')
#subprocess.Popen('cd .. && ' + new_user_in, shell=True)
但是当我运行它并输入 cd .. 我得到:
D:\Documents\Programmed\DesktopUnsorted
>
我不想要这个,因为我想要它做的是:
D:\Documents\Programmed\DesktopUnsorted>
编辑
我也试过了:new_location = new_location.replace(r'\n','')
但它并没有改变任何东西
谢谢, 斯蒂芬
【问题讨论】:
-
你可能也需要
new_location.replace(r'\n','')。 -
@Jean-FrançoisFabre 我已经试过了。但感谢您花时间提供帮助。我编辑了我的问题
标签: python python-2.7 subprocess