【问题标题】:Python, How can I correctly pass the result of one function (specifically, a file) to another function?Python,如何正确地将一个函数(特别是文件)的结果传递给另一个函数?
【发布时间】:2015-05-21 21:22:20
【问题描述】:

我是初学者。我用以下伪代码编写了一个 Python 程序:

  1. 定义函数1。

一个。这个函数需要一个大的单个 fasta 文件(一个基因组)并将其拆分为多个片段。

b.这些片段被写入一个 multi-fasta 输出文件(例如下面)。

  1. 定义函数2。

一个。该函数读取multi-fasta文件的行数

b.将 fasta id 后跟 fasta 条目的长度写入输出文件。

大部分代码:

from Bio import SeqIO
import io

def metagenome_simulator(genome_fasta, out_file):
    outfile = open(out_file, "a+b")
    fasta = SeqIO.parse(open(genome_fasta, "rU"), "fasta")
         #does the split, blah, blah - I know this function works on its own already
    len_file.close()
    fasta.close()
    return outfile

def contig_len_calculator(fasta, out_file):
    outfile = io.open(out_file, "wb")
    fhandle = io.open(fasta, "a+b")
    outfile.write("contig_id" + "\t" + "contig_length" + "\n")
    for entry in SeqIO.parse(fhandle, "fasta"):
        #calculates lengths, blah, blah - i know this works independently too
     outfile.close()
     fhandle.close()
     return

def main():
    output = metagenome_simulator(sys.argv[1], sys.argv[2])
    print(output)
    contig_len_calculator(output, sys.argv[3])

 main()

我的命令(bash shell)是:

./this_script.py genome_fasta_file split_fasta_out_file final_output_file.

输出将是两个单独的文件,一个用于程序中的每个函数。第一个是拆分fasta:

>split_1
ATCG....
>split_2
ATCG....
.
.
.

第二个是长度文件:

>split_1    300
>split_2    550
.
.
.

这不起作用。它可以很好地运行 Fuction1 并生成 split_fasta_output 文件,但随后返回:

<open file 'out_file', mode 'a+b' at 0x7f54b8454d20>
Traceback (most recent call last):
File "./this_script.py", line 62, in <module>
main()
File "./this_script.py", line 60, in main
contig_len_calculator(output, sys.argv[3])
File "./this_script.py", line 47, in contig_len_calculator
fhandle = io.open(fasta, "a+b")
TypeError: invalid file: <open file 'out_file', mode 'a+b' at 0x7f54b8454d20>

我不知道为什么它不起作用。所以我的问题是:如何正确地将在一个函数中创建的文件传递给另一个函数?

编辑:把整个回溯错误。

【问题讨论】:

  • 嘿布兰登,这里没有足够的信息来完全调试您的问题。能否包含 TypeError 的完整堆栈跟踪?
  • 已编辑,希望对您有所帮助。我基本上只是想知道如何在第一个函数中创建一个文件(我知道它工作得很好,并制作文件并放入 cwd),然后将该文件作为输入传递给第二个函数以执行其他操作给它。

标签: python function file ubuntu


【解决方案1】:

问题是metagenome_simulator 返回一个文件描述符,然后您尝试将其传递给io.openio.open 采用整数文件描述符 (some_fd.fileno()) 或路径。简单的解决方案是返回您的输出文件的路径,而不是输出文件本身。

def metagenome_simulator(genome_fasta, out_file):
    ...  # your code as-written
    return out_file

但如果你愿意,你也可以这样做:

def metagenome_simulator(genome_fasta, out_file):
    # completely as-written, including
    return outfile

def contig_len_calculator(fasta, out_file):
    outfile = io.open(out_file, "wb")
    fhandle = io.open(fasta.fileno(), "a+b")
    ...

第一种方法的优点是它使contig_len_calculatorout_filefasta 参数具有相同的类型,这看起来很合理。

【讨论】:

  • @kindall 你是对的!我认为len_file.close() 是将他的原始代码复制到 MCVE 时出错。我的答案已更新以阐明实际问题。
  • 代码还有很多内容(在我没有包含的原始函数中有几个参数),所以有几个人工制品。基本上我想不通的是如何在函数之间正确传递文件。感谢您的帮助!
  • @BrandonKieft 我强烈建议您执行f = open(filepath),传递它,然后再传递f.close()。但是,如果这是您想要使用的模式,那么您做得很好。 io.open 不接受文件对象,这是问题所在。
【解决方案2】:

open 函数接受一个文件名并返回一个文件对象。 metagenome_simulator 返回一个文件对象。您将其传递为fasta,然后在其上使用open。但是您不需要打开它,因为它已经是一个打开的文件,而不仅仅是一个文件名。

【讨论】:

    猜你喜欢
    • 2019-07-25
    • 2021-11-24
    • 2016-07-06
    • 2022-12-04
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多