A
A
Alexander Shilov2018-10-11 09:02:53
Python
Alexander Shilov, 2018-10-11 09:02:53

How to read text from all files in Python?

I have the following program code

from os import listdir

fileNames = listdir(r'C:\Users\sha\Desktop\27')
handleWrite = open(r'C:\Users\sha\Desktop\27\fileResult\Result.txt', 'w')
number = 0
for cycle in fileNames:
    q = fileNames[number]
    handle = open(r'C:\Users\sha\Desktop\27\{}'.format(q), 'r')
    data = handle.read(768)
    dataResult = data[755:768]
    print(dataResult)
    handle.close()
    handleWrite.write('{}\n'.format(dataResult))
    number +=1
handleWrite.close()

But for some reason it opens only the first 24 files, and then an error pops up
spoiler
5bbee77d9d903675963003.png

Can anyone suggest what could be wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Statkevich, 2018-10-11
@tabbols95

try encoding file = open(filename, encoding="utf8")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question