如何使用python更改用户的shell当前目录? (ubuntu / linux)

问题描述 投票:0回答:1

我们可以在shell中更改当前目录:

username@hostname:~/f1$ cd ~/f2
username@hostname:~/f2$ 

是否可以编写将当前目录更改为以编程方式确定的一个Python(v2.7和/或v3)?

例如

username@hostname:~/f1$ python change_dir.py
username@hostname:~/f2$ 
python shell directory cd
1个回答
0
投票

也许您的意思是这样的:

import os
os.chdir(os.path.join(os.path.dirname(os.getcwd()), 'f2'))
© www.soinside.com 2019 - 2024. All rights reserved.