关于在Python中插入shell命令
我在下面的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
就不管用了。我该怎么解决这个问题呢?
3 个回答
1
试试这个
commands.getoutput('zcat ' + fullpath + ' |wc -l')
因为在Python中,变量在字符串里不会自动展开。
3
fullpath
是一个变量,你需要把它和其他命令连接起来,像这样:
result = commands.getoutput('zcat ' + fullpath + ' |wc -l')
5
你需要把字符串和变量的值连接在一起:
result = commands.getoutput('zcat ' + fullpath + ' |wc -l')