如何在 python3 中创建包含 os.system 的函数?

How does one create a function containing os.system in python3?

我在 python 3 和 debian

我想要一个使用 os.system 的函数。为了简单起见,大致如下:

def notxt():
       command = "rm *.txt"
       os.system(command)
notxt()

但是当我 运行 脚本时,它没有执行命令就挂了。有没有办法解决这个问题,还是我处理不当?

我会在这里使用子进程,然后使用 运行。像这样

import subprocess

subprocess.run(["rm", "1.txt"])

您可能会找到另一个 bash 命令来删除所有 txt 文件

这是文档:

Python3 Docs for Subproccess