subprocess.call cd 不工作 [英] subprocess.call cd not working

查看:49
本文介绍了subprocess.call cd 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

In [3]: pwd

Out[3]: u'/Users/aarcher/Desktop/scripts'


In [5]: subprocess.call(['mkdir', '-p', os.path.expanduser('~/file/path/name')])

Out[5]: 0

我在另一个终端中验证它已成功创建/Users/aarcher/file/path/name,但无法更改到该目录,即使它返回 0:

I verified in another terminal that it had created /Users/aarcher/file/path/name successfully, but unable to change to that directory, even when it returns 0:

In [7]: subprocess.call(['cd', os.path.expanduser('~/file/path/name')], shell=True)

Out[7]: 0

In [8]: pwd

Out[8]: u'/Users/aarcher/Desktop/scripts'

我在unix盒子里

推荐答案

subprocess.call() 创建一个进程.cd 在那个进程中工作,但是当进程退出时它不会影响当前进程.这就是流程的设计方式.

subprocess.call() creates a new process. The cd works in that process, but when the process exits it won't affect the current process. This is how processes are designed to work.

如果您需要将脚本更改为其他目录,您可以使用 os.chdir 将改变当前进程的目录.

If you need your script to change to a different directory you can use os.chdir which will change the directory for the current process.

这篇关于subprocess.call cd 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆