简体   繁体   English

Python OS系统输入命令以在CLI应用程序中执行

[英]Python os.system enter commands to execute in CLI application

I am currently writing a program to help me with some database administration. 我目前正在编写一个程序来帮助我进行一些数据库管理。 It's supposed to execute commands in MariaDB. 应该在MariaDB中执行命令。 I thought since this is a CLI application I can just do that with os.system, but I'm having problems doing so. 我以为这是一个CLI应用程序,我可以使用os.system来做到这一点,但是这样做遇到了问题。 So lets say I have the following code 所以可以说我有以下代码

import os

os.system('mysql --user=%s --password=%s' %(user, password)
os.system('USE database;')

This code logs me into MariaDB, but it does not execute the second command to choose the database. 这段代码将我登录到MariaDB,但是它没有执行第二条命令来选择数据库。 Is this possible with os.system, and if not, what are my alternatives? 使用os.system是否可能,如果没有,我有什么选择? Thanks. 谢谢。

Edit: I tried using subprocess instead, which gives me another problem: It immediately exits MariaDB after logging in. 编辑:我尝试使用子进程代替,这给了我另一个问题:登录后立即退出MariaDB。

I recomand you to use Subprocess instead of os python module, using Popen 我建议您使用Popen使用Subprocess而不是os python模块

try to check this link out : Calling an external command in Python 尝试检查此链接: 在Python中调用外部命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM