【发布时间】:2021-07-25 10:09:22
【问题描述】:
我有一个 sed 命令,它应该在 linux 上的 python 代码中运行(使用 os.system() )或转换为 python 代码。但我不知道这个 sed 命令到底是做什么的。如果您给我代码或帮助我如何在 python 中使用 os.system 实现它,我们将不胜感激,因为我在使用 os.system 时遇到了很多错误。
sed -n '1~4s/^@/>/p;2~4p' file1.fastq > file1.fasta
顺便说一句,输入和输出文件应该在我的python代码中动态定义:
seq_file1 = '6448.fastq'
input_file1 = os.path.join(sys.path[0],seq_file1)
os.system(os.path.join("sed -n '1~4s/^@/>/p;2~4p' "+ seq_file1 + ' > ' + os.path.splitext(os.path.basename(input_file1))[0]+".fasta") , shell = True)
【问题讨论】:
-
使用
subprocess.run和朋友,而不是os.system。引入了 subprocess 模块来缓解使用os.system的许多问题。 -
为什么不用原生python重写而不是运行外部程序呢?
-
@9769953 问题是 subprocess.Popen 和 os.system 都无法识别字符串(sed命令)和变量的连接!你能应付吗?
-
@Shawn 我对sed命令不熟悉,所以不知道用原生python重写它有什么作用! :(
-
sed显式抑制所有输出(-n),然后显式打印每 4 行,从第一行开始,将行开头的@替换为 @987654330 @。然后它每第 4 行打印一次,从第 2 行开始。