有问题写入文件

时间:2015-12-23 01:36:29

标签: python file directory

我开始学习python并且在将n输出打印到桌面上的txt文件时遇到了一些麻烦。我希望它是Mac和Windows兼容。我在第4行的不存在或没有这样的文件目录的行中遇到语法错误。我正在尝试创建文本文件。

import os
os.path.join("~","Desktop")
output_file = open(os.path.join("~","Desktop","PythoBLASTout.txt"),"w")
from Bio import SearchIO
E_VALUE_THRES = 0.01
with open('/Users/evanclark/conesnail.xml', 'rU') as input:
    for qresult in SearchIO.parse(input, "blast-xml"):
        hits = qresult.hits
        query_id = qresult.id
        if len(hits) > 0:
            target_id = hits[0].id
            evalue = hits[0].hsps[0].evalue
            if evalue < E_VALUE_THRES:
                print("%s\t%s" % (query_id, target_id))


        #output_file.write("%s\t%s" % (query_id, target_id)

1 个答案:

答案 0 :(得分:1)

如果你想做代字号扩展,你需要告诉Python明确地这样做:

output_file = open(os.path.expanduser(os.path.join("~", ...)), "w")