Skip to main content
 首页 » 编程设计

python之关于将shell命令插入python

2025年05月04日13langtianya

我已经在 python 脚本中插入了一些 shell 命令,如下所示:

#!/usr/bin/python 
 
import os,sys,re 
import gzip 
import commands 
 
path = "/home/x/nearline" 
 
for file in os.listdir(path): 
  if re.match('.*\.recal.fastq.gz', file): 
    fullpath = os.path.join(path, file) 
    result = commands.getoutput('zcat fullpath |wc -l') 
    numseqs = int(result)/4.0 
  print numseqs 

zcat fullpath |wc -l 是插入的shell命令。

问题是,我在这里为所有fastq文件定义了fullpath,但是放在' '下之后,似乎是这个fullpath 不起作用。我该如何解决这个问题?

请您参考如下方法:

您必须将字符串与变量的值连接起来:

result = commands.getoutput('zcat ' + fullpath + ' |wc -l')