如何阻止subprocess.run()的输出?

huangapple go评论70阅读模式
英文:

How to prevent subprocess.run() output?

问题

我正在使用子进程模块来创建一些目录。但在某些情况下,相同的命令可能会在受限制的目录中创建目录。在这种情况下,我会在控制台上得到以下输出:mkdir: 无法创建目录 'location/to/directory': 权限被拒绝

如何避免将此输出显示在控制台上?

我尝试了以下命令:

subprocess.run(["mkdir", "-p", f"{outdir}/archive/backup_{curr_date}/"], check=True, stdout=subprocess.DEVNULL)
subprocess.run(["mkdir", "-p", f"{outdir}/archive/backup_{curr_date}/"], check=True, stdout=subprocess.PIPE)
subprocess.run(["mkdir", "-p", f"{outdir}/archive/backup_{curr_date}/"], check=True, capture_output=True)
英文:

I am using the subprocess module to create some directories. However in some cases the same command might be creating directories in restricted directories. In such cases I get an output to the console: mkdir: cannot create directory 'location/to/directory': Permission denied

How to avoid this output to the console?

I have tried the following commands:

subprocess.run(["mkdir", "-p", f"{outdir}/archive/backup_{curr_date}/"],check=True,stdout=subprocess.DEVNULL)
subprocess.run(["mkdir", "-p", f"{outdir}/archive/backup_{curr_date}/"],check=True,stdout=subprocess.PIPE)
subprocess.run(["mkdir", "-p", f"{outdir}/archive/backup_{curr_date}/"],check=True,capture_output=True)

答案1

得分: 1

更好的方法

os.makedirs 是创建新目录的更好方式。

可以这样使用:

try:
    os.makedirs(f"{outdir}/archive/backup_{curr_date}/", exist_ok=True)
except PermissionError as e:
    print(e)

它会在需要时创建 outdir 目录。


修复
> mkdir: 无法创建目录 'location/to/directory'

这不是标准输出,而是在控制台上打印的错误。首先,不应该抑制这个错误,而应该正确处理它。但在极端情况下,如果这是所需的结果,那么您应该像这样将 stderr 重定向到 PIPE

subprocess.run(["mkdir", "-p", f"{outdir}/archive/backup_{curr_date}/"], stderr=subprocess.PIPE)

一个可用的示例:

import subprocess

dir_path = "/usr/abc"

result = subprocess.run(["mkdir", "-p", dir_path], stderr=subprocess.PIPE)
error = result.stderr.decode()

if error:
    print(error)
# 输出: mkdir: /usr/abc: 操作不允许
英文:

Better Approach

os.mkdirs is much better way to create a new directory.

Which can be used this way:

try:
    os.makedirs(f"{outdir}/archive/backup_{curr_date}/", exist_ok=True)
except PermissionError as e:
    print(e)

It will create a outdir as well if it doesn't exist.


Fix
> mkdir: cannot create directory 'location/to/directory'

This is not standard output rather its error which gets printed on console. First of all, this error shouldn't be suppressed and handled properly. But in extreme case, if that's the required outcome then you should PIPE the stderr like this stderr=subprocess.PIPE

subprocess.run(["mkdir", "-p", f"{outdir}/archive/backup_{curr_date}/"], stderr=subprocess.PIPE)

A working example:

import subprocess

dir_path = "/usr/abc"

result = subprocess.run(["mkdir", "-p", dir_path], stderr=subprocess.PIPE)
error = result.stderr.decode()

if error:
    print(error)
# Output: mkdir: /usr/abc: Operation not permitted

huangapple
  • 本文由 发表于 2023年1月8日 22:37:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定