相当于Unix“ shell函数”的Powershell脚本

时间:2019-07-16 17:22:00

标签: python windows powershell batch-file

这基本上是Change the current directory from a Bash script的副本,除了我在Windows上并且希望能够从powershell脚本更改命令行目录。

我实际上是从powershell文件运行一个python脚本,解析该python脚本的输出目录,然后使用该输出作为要更改为的目录,因此是否有办法从python脚本中执行此操作而不是Powershell文件中的加分项!

要100%清除,是的,我知道“ cd”和“ os.chdir”,不是,不是我想要的-只会更改脚本或批处理文件的当前目录,而不是您更改的命令行正在运行!

代码如下(批处理文件是我的第一次尝试,我希望它是PS1脚本):

sw.bat -将父目录添加到sys $ PATH

@echo off
set _PY_CMD_="python C:\code\switch\switch.py %*"
for /f "tokens=*" %%f in ('%_PY_CMD_%') do (
    cd /d %%f
)

switch.py​​

import os
import argparse

LOCAL = r'C:\code\local'


def parse_args():
    parser = argparse.ArgumentParser(description='Workspace manager tool.')
    parser.add_argument('command', type=str, help='The command to execute, or workspace to switch to.')
    return parser.parse_args()

def execute_command(command):
    if command in ['l', 'local']:
        print(LOCAL)

def main():
    args = parse_args()
    execute_command(args.command)

if __name__ == '__main__':
    main()

4 个答案:

答案 0 :(得分:3)

如果我的问题正确,那么简化的方法将涉及使用[SS64]: FOR /F

script.py

target_dir = r"C:\Program Files"

print(target_dir)

script.bat

@echo off

set _PY_CMD="e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" script.py

for /f "tokens=*" %%f in ('%_PY_CMD%') do (
    pushd "%%f"
)

输出

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q057062514]> ver

Microsoft Windows [Version 10.0.18362.239]

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q057062514]> script.bat

[cfati@CFATI-5510-0:C:\Program Files]>
[cfati@CFATI-5510-0:C:\Program Files]> popd

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q057062514]>

注释

  • 我使用了 pushd (而不是 cd ),因为简单的 popd 会返回到原始目录。缺点是必须记住(堆栈)它们调用 pushd 的次数,以匹配对应的 popd 。如果要切换到 cd ,请像cd /d %%f
  • 那样使用它。

@ EDIT0

在开始时,问题仅针对 batch ,之后是 PowerShell PS )要求(可以等同于其他)问题)。
无论如何,这是一个简单的(虚拟)脚本,可以解决问题(我不是 PS 专家)。

script.ps1

$PyCmdOutput = & 'e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe' 'script.py'

Set-Location $PyCmdOutput

输出

PS E:\Work\Dev\StackOverflow\q057062514> .\script.ps1
PS C:\Program Files>

答案 1 :(得分:0)

在Python中尝试os.chdir

  

os.chdir(path)

    Change the current working directory to path. Availability: Unix, Windows.

答案 2 :(得分:0)

如果您要完全批量更改目录,这就是我要做的事情:

创建从其调用Python的Batch文件的主目录Var。 SET homeDir=%cd%

然后使用cd进入所需目录。程序进入目录后,使用start "Title here (optional)" cmd.exe

创建一个新的命令提示符。

最后,使用以下命令返回主目录 cd“%homeDir%”

脚本可能看起来像这样:

@echo off
REM using cd with no arguments returns the current directory path

SET homeDir=%cd%
REM set home Directory

cd "%desiredDirectory%"
REM moved directory

start "thisIsAName" cmd.exe
REM created new "shell" if you will

cd "%homeDir%"
REM returned to home directory
pause

答案 3 :(得分:0)

在CristiFati的帮助下,我将其制作为批处理文件。该解决方案已发布在问题中。经过更多的谷歌搜索之后,我将批处理文件解决方案转换为Powershell Script解决方案,如下所示:

$command = "& python C:\code\switch\switch.py $args"
Invoke-Expression $command | Tee-Object -Variable out | Out-Null
set-location $out

无需更改Python文件。 注意-此答案是为那些想要在python脚本中使用任意数量的参数并抑制stdout的用户提供的。