gpt4 book ai didi

Python - 使用子进程调用 sed?

转载 作者:太空狗 更新时间:2023-10-29 18:20:44 25 4
gpt4 key购买 nike

我希望使用子进程从 python 调用 sed。我尝试使用的脚本如下。但是,这会将 sed 输出通过管道传输到标准终端。似乎在我的 subprocess.call 语句中无法识别“>”运算符。有什么建议么?

import sys 
import os
import subprocess

files = os.listdir(sys.argv[1])

count = 0

for f in files:
count += 1
inp = sys.argv[1] + f
outp = '../' + str(count) + '.txt'
sub = subprocess.call(['sed', 's/\"//g', inp, '>', outp])

另外 - 我的文件名中有空格,即“file1.txt”。这可能是问题所在吗?当我从终端而不是脚本调用 sed 时,我的 sed 命令工作正常。

谢谢。

最佳答案

使用

out_file = open(outp, "w")
sub = subprocess.call(['sed', 's/\"//g', inp], stdout=out_file )

关于Python - 使用子进程调用 sed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6706953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com