bash - Python在退出前运行bash脚本
问题描述
我正在尝试用 python 调用一个 bash 脚本,但我需要它是最后执行的。
我想只要在我的脚本末尾添加这个就可以了:
val = subprocess.check_call("/scripts/files.sh '%s'" % title, shell=True)
但是它在它上面的代码之前被执行,为什么?
上面的最后几行:
print(q_1)
print(q_2)
print(q_3)
cursor.execute(q_1)
cursor.execute(q_2)
cursor.execute(q_3)
mariadb_connection.commit()
cursor.close()
mariadb_connection.close()
如果这很重要,我也会在所有这些代码之前使用val = subprocess.check_call
来运行另一个 bash 脚本
我如何确定我的脚本将是最后执行的事情?
解决方案
假设您的脚本看起来像
obj = MySqlSomething()
things
more things
val = subprocess.check_call(x)
如果该类MySqlSomething
有一个析构函数,它将在 之后调用check_call
,当您从脚本末尾掉出并obj
超出范围时。解决方法是让这种情况更早发生,只需将这些东西移动到一个函数中即可:
def main_main():
obj = MySqlSomething()
things
more things
main_main()
val = subprocess.check_call(x)
使用这种安排,obj
在结尾处超出范围main_main
。
推荐阅读
- reactjs - 如何用打字稿解决reactjs中的这个多重选择不起作用
- matplotlib - 如何确定 matplotlib.image.AxesImage 的 [fig] 大小(以像素为单位)?
- python - Python Colorama重置序列在unicode字符后添加空格
- javascript - 如何根据在 ngFor 中单击的父 div 来检查复选框?
- javascript - JS类别子类别 - 选择类别时隐藏子类别的值
- nearprotocol - 无法通过 RPC 接近协议事务状态
- android - Already generated AWS signedUrl not working with Android Retrofit2 call for upload videos(mp4)
- javascript - Check if string contains 3 or more ordered characters in javascript (no regex)
- python - scipy curve_fit 系数与预期值不一致(物理相关?)
- flutter - The argument type 'String' can't be assigned to the parameter type 'Uri' 'Uri' is from 'dart:core'. final response = await http.post(url,