windows 通过 Python 脚本更改提示工作目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/645864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing prompt working directory via Python script
提问by Oliver
Is it possible to change the Windows command prompt working directory via Python script?
是否可以通过 Python 脚本更改 Windows 命令提示符工作目录?
e.g.
例如
>> cd
>> c:\windows\system32
>> make_decision_change_dir.py
>> cd
>> c:\windows
I have tried a few things which don't work:
我尝试了一些不起作用的东西:
import os
os.chdir(path)
import os, subprocess
subprocess.Popen("chdir /D \"%s\"" %path, shell=True)
import os, subprocess
subprocess.Popen("cd \"%s\"" %path, shell=True)
import os, subprocess
subprocess.Popen("CD=\"%s\"" %path, shell=True)
As I understand it and observe these operations change the current processes working directory - which is the Python process and not the prompt its executing from.
据我了解并观察到这些操作会更改当前进程的工作目录 - 这是 Python 进程,而不是其执行的提示。
Thanks.
谢谢。
UPDATE
更新
The path I would want to change to is dynamic (based on what project I am working on, the full path to a build location changes) hence I wanted to code a solution in Python rather than hack around with a Windows batch file.
我想要更改的路径是动态的(基于我正在处理的项目,构建位置的完整路径更改)因此我想用 Python 编写解决方案,而不是使用 Windows 批处理文件。
UPDATE
更新
I ended up hacking a batch file together to do this ;( Thanks everyone.
我最终一起破解了一个批处理文件来做到这一点;(谢谢大家。
采纳答案by Ned Batchelder
I have a Python script to make moving around a file tree easier: xdir.py
我有一个 Python 脚本可以更轻松地在文件树中移动:xdir.py
Briefly, I have an xdir.py file, which writes Windows commands to stdout:
简而言之,我有一个 xdir.py 文件,它将 Windows 命令写入标准输出:
# Obviously, this should be more interesting..
import sys
print "cd", sys.argv[1]
Then an xdir.cmd file:
然后是一个 xdir.cmd 文件:
@echo off
python xdir.py %* >%TEMP%\__xdir.cmd
call %TEMP%\__xdir.cmd
Then I create a doskey alias:
然后我创建了一个 doskey 别名:
doskey x=xdir.cmd $*
The end result is that I can type
最终结果是我可以输入
$ x subdir
and change into subdir.
并更改为子目录。
The script I linked to above does much more, including remembering history, maintaining a stack of directories, accepting shorthand for directories, and so on.
我上面链接的脚本做的更多,包括记住历史、维护目录堆栈、接受目录的速记等等。
回答by Dave Webb
I'm not clear what you want to do here. Do you want a python script which you can run from a Windows command prompt which will change the working directory of the Windows command session?
我不清楚你想在这里做什么。你想要一个可以从 Windows 命令提示符运行的 python 脚本,它会改变 Windows 命令会话的工作目录吗?
If so, I'm 99.9% sure that's impossible. As you said yourself the python.exe
process is a separate process from the Windows cmd.exe
and anything you do in Python won't affect the Command prompt.
如果是这样,我 99.9% 肯定这是不可能的。正如您自己所说,该python.exe
进程是一个独立于 Windows 的进程,cmd.exe
您在 Python 中执行的任何操作都不会影响命令提示符。
There may be something you can do via the Windows API by sending keystrokes to the Windows or something but it would be pretty brittle.
您可能可以通过 Windows API 将按键发送到 Windows 或其他东西,但它会非常脆弱。
The only two practical options I can think of involve wrapping your Python script in a Batch file:
我能想到的仅有的两个实用选项涉及将 Python 脚本包装在批处理文件中:
- Output your desired directory from the Python script, read the output in your Batch file and CD to it.
- Start your Python script from a batch file, allow your Python script to start a new
cmd.exe
Window and get the Batch file to close the original Command window.
- 从 Python 脚本输出所需的目录,读取批处理文件和 CD 中的输出。
- 从批处理文件启动 Python 脚本,允许 Python 脚本启动一个新
cmd.exe
窗口并获取批处理文件以关闭原始命令窗口。
回答by cdleary
As people mentioned, child processes (i.e. your program) can't change the current working directory of a parent process (i.e. the terminal). This is why you need the two steps that everybody is describing. In most shells there's a way to make a macro or function to perform this two-step functionality.
正如人们所提到的,子进程(即您的程序)无法更改父进程(即终端)的当前工作目录。这就是为什么您需要每个人都在描述的两个步骤。在大多数 shell 中,有一种方法可以制作宏或函数来执行这两个步骤的功能。
For example, in bash
, you can make a single alias to compute the path and change the current working directory, similar to what @S.Lottdescribes for Windows:
例如,在 中bash
,您可以创建一个别名来计算路径并更改当前工作目录,类似于@S.Lott为 Windows 描述的内容:
alias my_cd='TMP=`compute_path.py`; cd $TMP;'
Note that the cd
command is still being interpreted in the parent process (the terminal), which has the ability to change its own current working directory.
请注意,该cd
命令仍在父进程(终端)中被解释,它有能力更改自己的当前工作目录。
回答by S.Lott
One common solution is a two-part script.
一种常见的解决方案是两部分脚本。
Part 1 is Python, which creates a temporary .BAT file that contains the appropriate CD command.
第 1 部分是 Python,它创建了一个包含适当 CD 命令的临时 .BAT 文件。
Part 2 is the temporary .BAT file.
第 2 部分是临时 .BAT 文件。
fancycd.bat
花式cd.bat
python figurethepath.py >temp.bat
temp.bat
回答by hi bye
imoprt os
os.system("start cmd.exe /k \"cd /d c:\windows\system32 & python make_decision_change_dir.py\"")
回答by gimel
The subprocess.Popen()
doc pagesays a child process will be created for the sub-process, so any working directory changes will be local to that subprocess.
该subprocess.Popen()
文档页面说,一个子进程将子进程来创建,所以任何工作目录变化将是局部的子进程。
If cwd is not None, the child'scurrent directory will be changed to cwd before it is executed. Note that this directory is not considered when searching the executable, so you can't specify the program's path relative to cwd.
如果 cwd 不是 None,则在执行之前,子节点的当前目录将更改为 cwd。请注意,搜索可执行文件时不考虑此目录,因此您不能指定程序相对于 cwd 的路径。
This will be the same for any changes done explicitly inside the subproceess, similar to the commands that appear in the question.
对于在子进程内显式完成的任何更改,这将是相同的,类似于问题中出现的命令。