首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带有更新变量的Python ()批处理文件

带有更新变量的Python ()批处理文件
EN

Stack Overflow用户
提问于 2022-11-28 19:24:31
回答 1查看 30关注 0票数 0

我想问如何在运行进程之前更新变量时运行外部批处理文件。我的问题的细节如下:

我现在有一个批处理文件,而它正在执行一个模拟过程。我想编写一个模块,在不手动更新批处理文件的情况下首先更新变量,然后运行模拟,最后导入结果,如下所示:

代码语言:javascript
运行
复制
# this will be the variable that I want to update
yyyy = 2022
mm = 11
dd = 28
Path1 = 'the path for first variable'
Path2 = 'the path for second variable'


# the batch file is like:
Batch_simulation.bat
Path 2/remote/noclear/Path 1/%yyyy%%mm%%dd%


# therefore, I want to update the variable in batch file first, then run the simulation, my code is looking like this right now:
import subprocess

yyyy = 2022
mm = 11
dd = 28
Path1 = 'the path for first variable'
Path2 = 'the path for second variable'

paramStr = str(yyyy)+','+str(mm)+','+str(dd)+','+Path1+','+Path2
bat_file = ['pathway for Batch_simulation.bat', paramStr]
process = subprocess.run([bat_file])
stdout, stderr = process.communicate()

有人能给我一些建议或任何可能的解决方案吗?非常感谢

EN

回答 1

Stack Overflow用户

发布于 2022-11-28 20:02:27

有人能给..。有没有可能的解决办法.?

  • 在循环中
    • 打开并读取批处理文件 (-->结果为字符串)
    • 使用模式匹配查找,然后用数据替换该字符串的相关部分
    • 将修改后的字符串写入批处理文件。
    • 运行带有子进程的批处理文件并获得其结果。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74605590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档