如何在Python中使用环境变量执行Windows批处理文件? [英] How do I execute a windows batch file using environment variable in Python?

查看:338
本文介绍了如何在Python中使用环境变量执行Windows批处理文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi,

I am working on a python code and require to open an executable which is located in my C drive. I dont want to code something like, 

    file = open("C:\\Documents\\CodeRepo...\\codeplay.exe)

I want to use the environment variable called LOCATION_HOME to point to this exe location.

I dont know when do i set this environment variable. Do i set it in my python code or using the terminal in Windows?







I am still having some issues. 

The thing is I provide some extra things in the Popen like shown below.

output = subprocess.Popen(('path -testroot C:\\....\Configuration -projectfile ProjectFilewithTags -environment'
), stdout=subprocess.PIPE).stdout

What I have tried:

<pre>
As of now, I did  <pre>path = os.environ["LOCATION_HOME"]
path += "\\binaries\\codeplay.exe" + "-testroot C:\\....\Configuration -projectfile ProjectFilewithTags -environment"





我打印出PYTHON的路径并且看起来很正确。但是当我在output = ....中打开文件时,系统找不到文件。



output = subprocess.Popen((path

),stdout = subprocess.PIPE).stdout

.

I PRINT OUT THE PATH IN PYTHON AND IT LOOKS CORRECT. but when I do the opening of the file in the output = .... the system cannot find the file.

output = subprocess.Popen((path
), stdout=subprocess.PIPE).stdout

推荐答案

我想这已经在前一个问题的评论中解决了。 />


当环境变量 HOME_LOCATION 有一个尾部反斜杠时,这可能会失败。



要处理这个 os.path.join 可以用来处理目录分隔符:

I guess this has been already solved within the comments of the previous question.

This might fail when the environment variable HOME_LOCATION has a trailing backslash.

To handle this os.path.join can be used which takes care of the directory separators:
path = os.environ["LOCATION_HOME"]
os.path.join(path, "binaries\\codeplay.exe")
path + " -testroot ..."


这篇关于如何在Python中使用环境变量执行Windows批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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