python为什么我不能从刚创建的文件中读取?

时间:2019-02-06 23:53:49

标签: python file

我正在尝试创建一个对某些单词进行排序的程序。 当我尝试重新打开我刚刚创建的文件时,没有任何反应。 如果我复制粘贴文件时使用其他名称,然后尝试将其打开就可以了。

    import itertools
    import codecs
    filepath = 'greekdic.txt'
    filepathtemp= 'temp.txt'
    filepathtemptwo = 'final.txt'
    filepathtempread = 'greekdic2.txt'
    ######################## INPUT #################################################
    chars = str(input("Enter letters: "))
    chars2=chars
    count = int(input("Enter lenght of word: "))
    count2=count
    x=0
    ######################## CREATE TEMP FILE WITH INTEREST WORDS #################
    file = codecs.open(filepathtemp, "w", "utf-8")
    with codecs.open(filepath, "r", "utf-8") as fp:  
       line = fp.readline().splitlines()
       cnt = 1
       while line:
        a=len(line[0])
        if a == count:
            print(line)
            file.write(line[0])
            file.write("\n")
        line = fp.readline().splitlines()
        cnt += 1
    ###########################################################################
    file = codecs.open(filepathtemptwo, "w", "utf-8")
    with codecs.open(filepathtemp, "r", "utf-8") as fp:  
       line = fp.readline().splitlines()
       cnt = 1
       while line:
        for x in range(count):
            if chars[x] == line[0][0]:
                print ("***********************************")
                print(line)
                file.write(line[0])
                file.write("\n")       
        line = fp.readline().splitlines()
        cnt += 1
    ########################## SEARCH ########################################
    print(chars)
    if __name__ == "__main__":
        for item in itertools.product(chars2, repeat=count2):
            ppp = "".join(item)
            #print(ppp)
---------->>>>>> ***with codecs.open(filepathtemptwo, "r", "utf-8") as fp2:***
                line1 = fp2.readline().splitlines()
                #print(line1[0])
                #print(ppp)
                cnt = 1
                while line1:
                    if str(ppp) == str(line1[0]):
                        print("--------------------------------------------")
                        print("value of line1 is: ")
                        print(line1[0])
                        print(id(line1[0]))
                        print("value of ppp is: ")
                        print(ppp)
                        print(id(ppp))
                    line1 = fp2.readline().splitlines()
                    cnt += 1

问题出在刚创建的“ filepathtemptwo”上。如果我复制粘贴内容在其他文件中就可以了。我什至使用新的变量名来更改。 谁能帮忙。

0 个答案:

没有答案