Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我正在用Python打开3个文件。两个读取的文件都很好;用开放的;但是写入文件只能使用;开放式;_Python_With Statement - Fatal编程技术网

我正在用Python打开3个文件。两个读取的文件都很好;用开放的;但是写入文件只能使用;开放式;

我正在用Python打开3个文件。两个读取的文件都很好;用开放的;但是写入文件只能使用;开放式;,python,with-statement,Python,With Statement,更新:当我今天尝试获取一个最小的程序时,我意识到有一个os.system('cmd/k')行可能导致了这个问题。我不知道为什么,但当我使用打开的时,它必须自动关闭文件@谢谢你的建议 我是python新手,所以我可能缺少一些基本的东西,尽管我已经运行了教程并搜索了“with”和“open”。在下面的两段代码中,除了以下几行之外,不应更改任何内容(代码的其余部分主要是注释,我在其中勾勒出了我最终要做的事情以及变量声明和初始化): 将open(fileDirectory+sigMatchFile,“w

更新:当我今天尝试获取一个最小的程序时,我意识到有一个
os.system('cmd/k')
行可能导致了这个问题。我不知道为什么,但当我使用打开的
时,它必须自动关闭文件@谢谢你的建议

我是python新手,所以我可能缺少一些基本的东西,尽管我已经运行了教程并搜索了“with”和“open”。在下面的两段代码中,除了以下几行之外,不应更改任何内容(代码的其余部分主要是注释,我在其中勾勒出了我最终要做的事情以及变量声明和初始化):

将open(fileDirectory+sigMatchFile,“w”)作为sigMatch:
,它不会在文件中给出输出

sigMatch=open(fileDirectory+sigMatchFile,“w”)
在文件中给出输出

还有缩进(我回去试着确保空行缩进与编程行匹配,虽然我不确定这是个问题,但没有帮助。)

“with open”适用于读取文件,但我无法使其适用于写入文件


使用with open for sigMatch:生成空文件的代码。
使用openforsigmatch:生成具有写入输出的文件的代码。
我不确定问题出在哪里,但我只想指出,
close
在第一段代码中不是必需的。使用
with
with
with file objects的原因是
with
在退出
with
块时会自动为您关闭文件。谢谢-我确实意识到
with
将关闭文件-我只是试图使代码段尽可能相似,看看我是否能找出为什么它没有关闭工作我正在认真考虑只使用
open
来编写文件,但我担心它稍后会咬到我,因为有一些环境问题我不明白。顺便说一下:我建议使用
os.path.join()
而不是自己连接字符串来获取文件路径。我有一个猜测,它不会刷新,但事实上它会刷新,这是有道理的。我不确定是什么问题。请提供一个。以防万一,如果fileDirectory代表python文件所在的文件夹,则可以使用“.\”+sigMatchFile。
with open(fileDirectory+sigMatchFile, "w") as sigMatch:
    sigMatch.write("1 - Testing if opened\n")

    with open(fileDirectory+currentFastaFile, "r") as fasta:
        fl = fasta.readline().split("|")
        currentGene = fl[1]
        fasta.close()
    sigMatch.write("2 - Testing if still open\n")

    with open(fileDirectory+blastpFile, "r")as blastp:
        blpl = blastp.readlines()
        for x in blpl:
            if "significant alignments:" in x:
                print ("significant alignments: " + x)
                sigMatch.write("3 - Testing if still open\n")
                sigMatch.write("significant alignments: " + x +"\n")

        blastp.close()
    sigMatch.close()
sigMatch = open(fileDirectory+sigMatchFile, "w")
sigMatch.write("1 - Testing if opened\n")

with open(fileDirectory+currentFastaFile, "r") as fasta:
    fl = fasta.readline().split("|")
    currentGene = fl[1]
    fasta.close()
sigMatch.write("2 - Testing if still open\n")

with open(fileDirectory+blastpFile, "r")as blastp:
    blpl = blastp.readlines()
    for x in blpl:
        if "significant alignments:" in x:
            print ("significant alignments: " + x)
            sigMatch.write("3 - Testing if still open\n")
            sigMatch.write("significant alignments: " + x +"\n")

    blastp.close()
sigMatch.close()