CD在航站楼运行的Python脚本后,给定目录? [英] cd Terminal at a given directory after running a Python script?

查看:126
本文介绍了CD在航站楼运行的Python脚本后,给定目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个简单的Python脚本,可以使用和/或操作系统来执行某些命令,这是工作的罚款。

I'm working on a simple Python script that can use subprocess and/or os to execute some commands, which is working fine.

然而,当脚本退出,我想 CD 实际的终端(在这种情况下,OS X)等退出,新的文件准备使用其中已创建的目录中。以下所有的( subprocess.Popen 使用os.system os.chdir )可以做什么,我是从的希望脚本(即它们在目标目录),但在退出离开码头在的脚本本身的目录下执行的东西,不是的目标的目录。

However, when the script exits I'd like to cd the actual Terminal (in this case OS X) so on exit, the new files are ready to use in the directory where the have been created. All the following (subprocess.Popen, os.system, os.chdir) can do what I want from within the script (i.e. they execute stuff in the target directory) but on exit leave the Terminal at the script's own directory, not the target directory.

我想避免写一个shell脚本来临时文件只是为了实现这一点,如果这是在所有可能呢?

I'd like to avoid writing a shell script to temporary file just to achieve this, if this is at all possible anyway?

推荐答案

可悲的是,没有。流程不允许改变他们的父进程的环境,在这种情况下你的Python脚本是shell的一个子进程。你可以假,它通过让你的Python程序设置了一个新的外壳 - 调用子打开一个shell进程和$ P $它psent给用户,从自身继承修改后的环境 - 但有强迫的Python的缺点过程持续运行。

Sadly, no. Processes are not allowed to change the environment of their parent process, and in this case your Python script is a child process of the shell. You could "fake" it by having your Python process set up a new shell - call subprocess to open a shell process and present it to the user, inheriting the modified environment from itself - but that has the downside of forcing the Python process to run continually.

这是真的是shell脚本是.. :-)有人显然需要写一个更传统的外壳(例如距离小于IPython的抨击),它可以使用Python作为脚本语言。

This is really what shell scripts are for.. :-) Someone clearly needs to write a more traditional shell (e.g. closer to Bash than IPython) which can use python as its scripting language.

这篇关于CD在航站楼运行的Python脚本后,给定目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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