免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5299 | 回复: 5
打印 上一主题 下一主题

新手请教python程序返回值的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-11-01 10:37 |只看该作者 |倒序浏览
麻烦各位大侠帮看看这个程序的返回值为啥不是零???谢谢!

#! /usr/bin/python

import re
import os
import sys
import gzip

def rmN(Args):
        if not Args:
                print 'Usage: rmN <in.fq> <max_N_ratio><outfile>'
                print 'Max_N_ratio [float] , e.g. 0.05'
                sys.exit(0)
        if re.search('gz$|gzip$', Args[0]):
                infile = gzip.open(Args[0], 'rb')
        else:
                infile = open(Args[0], 'r')
        maxN = float(Args[1])
        outfile = Args[-1]
        #fqname = os.path.basename(Args[0]).replace('.gz', '')
        #fqname = fqname.replace(fqname.split('.')[-1], 'fq')
        remain = open(outfile, 'w')
        #remove = open(Args[-1] + '/removed_N_' + fqname, 'w')
        #remove_log  = open(outfile + '.log', 'w')
        remove_reads = 0
        total_reads = 0
        while True:
                readid = infile.readline().rstrip()
                if len(readid) == 0:
                        break
                seq = infile.readline().rstrip()
                mark = infile.readline().rstrip()
                qual = infile.readline().rstrip()
                seqlen = len(seq)
                total_reads += 1
                N_count = seq.count('N')
                N_ratio = float(N_count) / seqlen
                if N_ratio <= maxN:
                        remain.write('%s\n%s\n%s\n%s\n' %(readid, seq, mark, qual))
#            remain.write('%s\n%s\n%s\n%s\n' %(readid, seq, mark, qual))
                else:
                        remove_reads += 1
        remove_rate = float(remove_reads) / total_reads * 100
        sys.stdout.write('Total Reads\tRemoved N Reads\t%Removed N Rate\n')
        sys.stdout.write('%s\t%s\t%.2f\n' %(total_reads, total_reads - remove_reads, remove_rate))
    #remove_log.write('Total Reads\tRemoved N Reads\t%Removed N Rate\n')
        #remove_log.write('%s\t%s\t%.2f\n' %(total_reads, total_reads - remove_reads, remove_rate))
        infile.close()
        remain.close()


if __name__ == '__main__':
        if len(sys.argv) > 1:
                rmN(sys.argv[1:])
        #sys.exit(0)
        else:
                print 'Usage: rmN <in.fq> <max_N_ratio><outfile>'
                print 'Max_N_ratio [float] , e.g. 0.05'
                sys.exit(0)



论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每月发帖之星
日期:2015-09-11 19:30:52IT运维版块每周发帖之星
日期:2015-09-11 19:20:31IT运维版块每日发帖之星
日期:2015-08-26 06:20:00每日论坛发贴之星
日期:2015-08-20 06:20:00IT运维版块每日发帖之星
日期:2015-08-20 06:20:002015年辞旧岁徽章
日期:2015-03-03 16:54:15金牛座
日期:2014-05-04 16:58:09双子座
日期:2013-12-17 16:44:37辰龙
日期:2013-11-22 15:20:59狮子座
日期:2013-11-18 22:55:08射手座
日期:2013-11-12 10:54:26
2 [报告]
发表于 2013-11-01 13:25 |只看该作者
什么的返回值?return

论坛徽章:
0
3 [报告]
发表于 2013-11-01 16:10 |只看该作者
def run_cmd(cmd_str):
    """
   Throw exception if run command fails
    """
    #process = subprocess.Popen(cmd_str, stdout = subprocess.PIPE,stderr = subprocess.PIPE, shell = True)
  process = subprocess.Popen(cmd_str, stdout = subprocess.PIPE,stderr = subprocess.STDOUT, shell = True)
    stdout_str, stderr_str = process.communicate()
    if process.returncode != 0:
  raise Exception("Failed to run '%s'\n%s%sNon-zero exit status %s" %(cmd_str, stdout_str, stderr_str, process.retu
    rncode))

我定义了这样一个子函数,用来运行上面那个程序总是会报错Non-zero exit status 126

论坛徽章:
13
丑牛
日期:2013-08-16 15:08:22技术图书徽章
日期:2013-11-26 10:13:40双鱼座
日期:2013-11-08 15:03:26戌狗
日期:2013-11-08 13:52:30技术图书徽章
日期:2013-11-05 14:06:30戌狗
日期:2013-10-31 11:45:42CU十二周年纪念徽章
日期:2013-10-24 15:41:34天秤座
日期:2013-10-11 14:55:08子鼠
日期:2013-09-26 19:36:35水瓶座
日期:2013-09-26 17:44:56午马
日期:2013-08-26 10:24:23丑牛
日期:2013-08-19 14:43:22
4 [报告]
发表于 2013-11-01 17:57 |只看该作者
回复 3# ganhy


    不可以这样赋值
  1. stdout_str, stderr_str = process.communicate()
复制代码
process.communicate()返回的是一个数组

论坛徽章:
0
5 [报告]
发表于 2013-11-04 14:06 |只看该作者
回复 4# 代号:军刀


    我运行其他的函数时都没有报这种错啊

论坛徽章:
0
6 [报告]
发表于 2013-11-04 14:06 |只看该作者
回复 4# 代号:军刀


    我运行其他的函数时都没有报这种错啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP