glob error <_io.TextIOWrapper name='…' mode='r' encoding='cp1252'> reading text file error

后端 未结 1 555
失恋的感觉
失恋的感觉 2021-02-04 21:37

I am trying to make a social program where the profiles are stored in .txt files here is part of the code:

XX = []
pl = glob.glob(\'*.txt\')
for a in pl:
     if         


        
相关标签:
1条回答
  • 2021-02-04 21:57

    The open method opens the file and returns a TextIOWrapper object but does not read the files content.

    To actually get the content of the file, you need to call the read method on that object, like so:

    G = open(P, 'r')
    print(G.read())
    

    However, you should take care of closing the file by either calling the close method on the file object or using the with open(...) syntax which will ensure the file is properly closed, like so:

    with open(P, 'r') as G:
        print(G.read())
    
    0 讨论(0)
提交回复
热议问题