fnmatch在不同的脚本中工作不一致

时间:2015-11-18 01:25:26

标签: python file-handling

我正在编写一个python脚本,它将为我使用的分析程序编写输入文件。其中一个步骤是获取文件名列表并搜索输入目录,打开它们,并从中获取一些信息。我在一个测试脚本中使用os.walkfnmatch编写了以下内容,该脚本中包含了感兴趣的目录,并且工作得很好:

for locus in loci_select: # for each locus we'll include
    print("Finding file " + locus)
    for root, dirnames, filenames in os.walk('../phylip_wigeon_mid1_names'):
        for filename in fnmatch.filter(filenames, locus): # look in the input directory
            print("Found file for locus " + locus + " in set")
            loci_file = open(os.path.join('../phylip_wigeon_mid1_names/', filename))
            with loci_file as f:
                for i, l in enumerate(f):
                    pass
             count = (i) * 0.5 # how many individuals present
             print(filename + "has sequences for " + str(count) + " individuals")

......依此类推(其他一切都有效,所以我会饶你)。

虽然我把它放入更大的脚本并切换输入参数的目录名称,但它似乎停止在第三和第四行之间工作,尽管几乎相同:

    for locus in use_loci: # for each locus we'll include
        log.info("Finding file " + locus)
        for root, dirnames, filenames in os.walk(args.input_dir):
            for filename in fnmatch.filter(filenames, locus): # look in the input directory
                 log.info("Found file for locus " + locus + " in set")
                 loci_file = open(os.path.join(args.input_dir, filename))
                 with loci_file as f:
                     for i, l in enumerate(f):
                        pass
                 count = (i) * 0.5 # how many individuals present
                 log.info(filename + "has sequences for " + str(count) + " individuals")

我在可疑线路之间使用临时打印声明对其进行了测试,看起来它们似乎是罪魁祸首,因为我的屏幕输出看起来像:

2015-11-17 15:53:20,505 - write_ima2p_input_file - INFO - Getting selected loci for analysis
2015-11-17 15:53:20,505 - write_ima2p_input_file - INFO - Finding file uce-7999_wigeon_mid1_contigs.phy

2015-11-17 15:53:20,629 - write_ima2p_input_file - INFO - Finding file uce-4686_wigeon_mid1_contigs.phy

 2015-11-17 15:53:20,647 - write_ima2p_input_file - INFO - Finding file uce-5012_wigeon_mid1_contigs.phy

......等等。

我已尝试切换到glob,以及简单的事情,例如重新排列此部分属于我的较大代码,但没有任何工作。任何见解都会非常感激!

0 个答案:

没有答案