简体   繁体   English

如何更改python中的目录,使其在运行脚本后保持不变?

[英]How do I change directory in python so it remains after running the script?

I'm trying to change the terminal directory through a python script.我正在尝试通过 python 脚本更改终端目录。 I've seen this post and others like it so I know about os.chdir, but it's not working the way I'd like.我看过这篇文章和其他人喜欢它,所以我知道 os.chdir,但它没有按照我想要的方式工作。 os.chdir appears to change the directory, but only for the python script. os.chdir 似乎更改了目录,但仅适用于 python 脚本。 For instance I have this code.例如我有这个代码。

#! /usr/bin/env python
import os

os.chdir("/home/chekid/work2/")
print os.getcwd()

Unfortunately after running I'm still in the directory of the python script (eg /home/chekid) rather than the directory I want to be in. See below.不幸的是,运行后我仍然在 python 脚本的目录中(例如 /home/chekid)而不是我想要的目录。见下文。

gandalf(pts/42):~> pwd
/home/chekid

gandalf(pts/42):~> ./changedirectory.py
/home/chekid/work2

gandalf(pts/42):~> pwd
/home/chekid

Any thoughts on what I should do?关于我应该做什么的任何想法?

Edit : Looks like what I'm trying to do doesn't exist in 'normal' python.编辑:看起来我正在尝试做的事情在“普通”python 中不存在。 I did find a work around, although it doesn't look so elegant to me.我确实找到了解决方法,尽管它对我来说看起来并不那么优雅。

 cd `./changedirectory.py`

You can't.你不能。 The shell's current directory belongs to the shell, not to you. shell 的当前目录属于 shell,而不是你。

(OK, you could ptrace(2) the shell and make it call chdir(2) , but that's probably not a great design, won't work on Windows, and I would not begin to know how to do it in pure Python except that you'd probably have to mess around with ctypes or something similar.) (好吧,你可以ptrace(2) shell 并让它调用chdir(2) ,但这可能不是一个很好的设计,不能在 Windows 上工作,而且我不会开始知道如何在纯 Python 中做到这一点,除了你可能不得不弄乱 ctypes 或类似的东西。)

You could launch a subshell with your current working directory.您可以使用当前工作目录启动子shell。 That might be close enough to what you need:这可能足够接近您的需要:

os.chdir('/path/to/somewhere')
shell = os.environ.get('SHELL', '/bin/sh')
os.execl(shell, shell)
# execl() does not return; it replaces the Python process with a new shell process

The original shell will still be there, so make sure you don't leave it hanging around.原始外壳仍然存在,因此请确保不要将其留在周围。 If you initially call Python with the exec builtin (eg exec python /path/to/script.py ), then the original shell will be replaced with the Python process and you won't have to worry about this.如果您最初使用exec内置exec (例如exec python /path/to/script.py )调用 Python,那么原始 shell 将被 Python 进程替换,您不必担心这一点。 But if Python exits without launching the shell, you'll be left with no shell open at all.但是如果 Python 在没有启动 shell 的情况下退出,你将根本没有打开 shell。

The current working directory is an attribute of a process.当前工作目录是进程的一个属性。 It cannot be changed by another program, such as changing the current working directory in your shell by running a separate Python program.它不能被另一个程序更改,例如通过运行单独的 Python 程序来更改 shell 中的当前工作目录。 This is why cd is always a shell built-in command.这就是为什么cd总是一个 shell 内置命令。

You can if you cheat: Make a bash script that calls your python script.如果你作弊,你可以:制作一个调用你的 python 脚本的 bash 脚本。 The python script returns the path you want to change directory to. python 脚本返回要更改目录的路径。 Then the bash script does the acctual chdir.然后bash脚本执行acctual chdir。 Of course you would have to run the bash script in your bash shell using "source".当然,您必须使用“source”在 bash shell 中运行 bash 脚本。

You can make your python print the directory you want to move to, and then call your script with cd "$(./python-script.py)".你可以让你的 python 打印你想要移动到的目录,然后用 cd "$(./python-script.py)" 调用你的脚本。 In condition your script actually does not print anything else.在这种情况下,您的脚本实际上不打印任何其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从python脚本更改当前目录? - How do I change my current directory from a python script? 如何从脚本中找到Python运行脚本的目录? - How do I find directory of the Python running script from inside the script? 如何在Python中运行bash脚本,但就好像它从另一个目录运行? - How do I run a bash script inside Python, but act as if it's running from another directory? Python,如何在本地更改目录? - Python, How do I change the directory locally? 如何杀死正在运行的Python脚本? - How do I kill a running Python Script? 如何在 Python 中引用一个目录,使其在多台计算机上工作? - How do I reference a directory in Python so it works on multiple computers? 在运行 python 脚本之前,如何让 shell_exec() 更改环境 - How do I get shell_exec() to change the environment before running a python script 在PyCharm中运行Python脚本后,如何保存要保存的变量值? - How do I make variable values to be saved after running a Python script in PyCharm? 使用Python的Selenium,如何在运行脚本后获取页面输出? - Selenium with Python, how do I get the page output after running a script? 如何检查目录是否存在,然后检查是否在Python中提供了新的目录名称? - How do I check if a directory exists and then, if so, ask for a new directory name in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM