简体   繁体   English

使用Python从文件读取

[英]Reading From File in Python

I was wondering if somebody could help me out with the following Python 3 code. 我想知道是否有人可以通过以下Python 3代码来帮助我。 For some reason it is not doing what I believe it should be doing and I cannot understand why. 由于某种原因,它没有按照我认为应该做的去做,而且我不明白为什么。

with open("ModuleShow.txt", "w+", encoding='utf-8') as ModuleShowFile:
            if ModulesLeft == 0:
                ModuleLoaded = CommandLine[2]
                print(ModuleLoaded)
            else:
                ModuleLoaded = ModuleToLoad[0]
                ModuleToLoad.pop(0)
                ModulesLeft = ModulesLeft - 1
                #            ModuleFile.write("\n" + ModuleLoaded)                                                    
                #            CommandsFile.write("module show" + ModuleLoaded)                                         
            output = subprocess.Popen(["bash", "-ci", "module show " + ModuleLoaded], stderr=ModuleShowFile)

#            with open("ModuleShow.txt", "r", encoding='utf-8') as ModuleShowFile:                                        

            print(ModuleShowFile.read())
            for ModuleFileLine in ModuleShowFile:
                FileLine = ModuleFileLine.split(" ")

                print(FileLine[0])
                if FileLine[0] == "prepend-path":
                    print(FileLine[0])
                    if FileLine[1] == "PATH":
                        ModulePathFile.write(FileLine[2] + " " + ModuleLoaded + "\n")
                    elif FileLine[0] == "module":
                        ModuleToLoad.append(FileLine[2])
                        ModulesLeft = ModulesLeft + 1

So what I am doing in the above code is writing information to a file called ModuleShow.txt and then parse that information and writing the parsed information to a file called ModulePath.txt, I opened the ModulePath.txt before, but I did not include that part of the code since I believe it is not related to the problem I am having. 因此,我在上面的代码中所做的工作是将信息写入名为ModuleShow.txt的文件,然后解析该信息并将解析后的信息写入名为ModulePath.txt的文件,我之前打开过ModulePath.txt,但并未包括这部分代码,因为我认为它与我遇到的问题无关。

So basically, when I run my script, the contents are written to the file, but I do not get anything when I read the contents. 因此,基本上,当我运行脚本时,会将内容写入文件,但是在读取内容时却什么也没得到。 For example, I tried using "print(ModuleShowFile.read())," but nothing prints yet the file gets created with the contents inside. 例如,我尝试使用“ print(ModuleShowFile.read())”,但是什么也不会打印,但是文件的内容是内部创建的。 I tried having two "with open()" lines, one for writing and another for reading, but it still did not solve the problem. 我尝试使用两行“ with open()”行,一行用于编写,另一行用于阅读,但是仍然不能解决问题。 Hopefully it is something simple and I apologize for the long post. 希望这很简单,对于冗长的帖子,我深表歉意。 I would greatly appreciate any help. 我将不胜感激任何帮助。 Thank you. 谢谢。

Do you even need to write the file ModuleShow.txt? 您是否还需要编写ModuleShow.txt文件? Or can you just hold the entries in a list? 还是可以仅将条目保留在列表中?

If you really need to read them back in again, close the file first, like this: 如果确实需要再次读回它们,请先关闭文件,如下所示:

with open("ModuleShow.txt", "w+", encoding='utf-8') as ModuleShowFile:
    # ... whatever (I'm assuming you're writing to the file here)

# now the file is closed
with open("ModuleShow.txt", "r", encoding='utf-8') as ModuleShowFile:
    for ModuleFileLine in ModuleShowFile:
        # ... now you can read each line

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM