使用subprocess.run的py程序can not open file问题剖析

背景:

开发一个调用openSeespy的GUI软件,通过在界面中点击读取文件,可直接执行openseespy文件

举例:

我在Pycharm上直接运行openSeespy官网的例子(链接如下),

14.2.1. Cantilever 2D EQ ground motion with gravity Analysis — OpenSeesPy 3.5.1.3 documentation

py文件与地震动数据放在一个文件夹下,通过Pycharm右击运行是运行正确的

=========================================================
Start cantilever 2D EQ ground motion with gravity example
WARNING - the 'fullGenLapack' eigen solver is VERY SLOW. Consider using the default eigen solver.u2 =  -0.07441860465116278
Passed!
=========================================

但是呢,我在其他py文件中使用subprocess.run会提示

代码是这么写的,

    def onRunPy(self):
        path_py = self.InputPy.text().strip()  # 删除空白符
        (filepath, filename) = os.path.split(path_py)  # 分离文件路径与文件名
        (name, suffix) = os.path.splitext(filename)  # 分离文件名与后缀

        run(['python', path_py])

 报错是这么的:

=========================================================
Start cantilever 2D EQ ground motion with gravity example
WARNING - PathSeries::PathSeries() - could not open file A10000.dat
WARNING invalid accel series: 2 pattern UniformExcitation -accel {series}
WARNING failed to create pattern
Traceback (most recent call last):
  File "D:\工作文件\软件平台开发\examples\Canti2DEQ.py", line 28, in <module>
    class Canti2DEQ():
  File "D:\工作文件\软件平台开发\examples\Canti2DEQ.py", line 72, in Canti2DEQ
    pattern('UniformExcitation', 2, 1, '-accel', 2)		          # define where and how (pattern tag, dof) acceleration is applied
opensees.OpenSeesError: See stderr output

解决方案:

将openSeespy例子中的,读取文件的代码进行修改

 timeSeries('Path', 2, '-dt', 0.005, '-filePath', 'A10000.dat', '-factor', G)
 timeSeries('Path', 2, '-dt', 0.005, '-filePath', '../examples/A10000.dat', '-factor', G)

文件路径进行修改,修改到什么情况呢?保证主程序(我这里是开发的软件)能找到

延伸问题:

1.这个subprocess开了一个子进程,为什么还是以主进程为基准找这个.dat文件?

2.当用run跑完程序后,怎么才能让子程序将建立好的模型传回给主程序呢?

3.我试过将.dat文件路径写成绝对路径,为什么执行依旧会报错?

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值