Python - 使用子进程调用sed?

2023-12-27

我想打电话sed从 python 使用子进程。我尝试使用的脚本如下。但是,这会将 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 命令工作正常。

Thanks.


Use

out_file = open(outp, "w")
sub = subprocess.call(['sed', 's/\"//g', inp], stdout=out_file )
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python - 使用子进程调用sed? 的相关文章

随机推荐