【发布时间】:2012-08-30 02:18:57
【问题描述】:
实际上需要走一些路径并执行一些命令,下面是代码
代码:
import os
present_working_directory = '/home/Desktop/folder'
目前我在folder
if some_condition == true :
change_path = "nodes/hellofolder"
os.chdir(change_path)
print os.getcwd()
if another_condition == true:
change_another_path = "nodes"
os.chdir(change_another_path)
print os.getcwd()
**Result**:
'/home/Desktop/folder/nodes/hellofolder'
python: [Errno 1] No such file or directory
实际上这里发生的事情是当我第一次使用os.chdir()时,目录已更改为
'/home/Desktop/folder/nodes/hellofolder',
但是对于第二个,我需要通过移回一个文件夹来运行文件
'/home/Desktop/folder/nodes'
谁能告诉我如何在python中将一个文件夹移回
【问题讨论】:
-
尽可能避免使用
os.chdir。subprocess模块的函数将工作目录作为参数。 (另外,true应该是True和== True从来没有必要。) -
@Kour ipm,正如 larsmans 所说,使用子进程做你需要做的事情,它有关键字 cwd。所以调用你需要的东西: subprocess.call("yourCommand", shell=True, cwd="path/to/directory")