为什么我的代码不会写入文件输出?赋值前引用的局部变量
Why wont my code write to the file output? local variable referenced before assignment
我编写了一些代码来读取我的文本文件,找到一组与基因相关联的数字,然后使用这些数字来搜索基因本身,这样我就可以提取一堆包含每个基因的文本文件。
我已经成功地得到了数字,但是我在写入文件时遇到了问题。我收到错误“赋值前引用的局部变量 'gene_substring'”。我做了一些研究并尝试使用 global 来修复它,但它在其他地方抛出了错误。
#function to extract the genes by using the numbers in my list
end_file = "/Users...."
def extract_genes(start_stop, genome_sequence):
for start,stop in start_stop:
# extracts start:stop gene from the sequence
if start > stop:
gene_substring = genome_sequence[0:start] + genome_sequence[stop:]
# store in file
with open(end_file + "/" + name + "+" + ".txt", "w",) as file:
file.write(gene_substring)
#My code to get the output
work_dir = "/Users/"
for path in glob.glob(os.path.join(work_dir, "*.gbff")):
numbers = extract_numbers(path)
sequences = extract_seq(path)
extract_genes(start_stop, sequences)
print(path)
我该如何解决这个问题?谢谢:)
变量gene_substring
只有在条件start > stop
为真时才被初始化。但是,如果不满足要求怎么办?你必须初始化变量gene_substring
,或者简单地移动这个
with open(end_file + "/" + name + "+" + ".txt", "w",) as file:
file.write(gene_substring)
进入语句if start > stop:
此外,请确保 if 语句正确
我编写了一些代码来读取我的文本文件,找到一组与基因相关联的数字,然后使用这些数字来搜索基因本身,这样我就可以提取一堆包含每个基因的文本文件。 我已经成功地得到了数字,但是我在写入文件时遇到了问题。我收到错误“赋值前引用的局部变量 'gene_substring'”。我做了一些研究并尝试使用 global 来修复它,但它在其他地方抛出了错误。
#function to extract the genes by using the numbers in my list
end_file = "/Users...."
def extract_genes(start_stop, genome_sequence):
for start,stop in start_stop:
# extracts start:stop gene from the sequence
if start > stop:
gene_substring = genome_sequence[0:start] + genome_sequence[stop:]
# store in file
with open(end_file + "/" + name + "+" + ".txt", "w",) as file:
file.write(gene_substring)
#My code to get the output
work_dir = "/Users/"
for path in glob.glob(os.path.join(work_dir, "*.gbff")):
numbers = extract_numbers(path)
sequences = extract_seq(path)
extract_genes(start_stop, sequences)
print(path)
我该如何解决这个问题?谢谢:)
变量gene_substring
只有在条件start > stop
为真时才被初始化。但是,如果不满足要求怎么办?你必须初始化变量gene_substring
,或者简单地移动这个
with open(end_file + "/" + name + "+" + ".txt", "w",) as file:
file.write(gene_substring)
进入语句if start > stop:
此外,请确保 if 语句正确