I'm trying to read a file in Python using Win32Api so as to be able to open the file without locking it on a Windows system.
I've been able to open the file and even to read from it but when I try to implement the iterator protocol I get an error message that I can't understand.
Here's an example script that reproduce the problem
#!/usr/bin/env python
import os
class FileTail(object):
def __init__(self, file):
self.open(file)
def open(self, file):
"""Open the file to tail and initialize our state."""
fh = None
import win32file
import msvcrt
handle = win32file.CreateFile(file,
win32file.GENERIC_READ,
win32file.FILE_SHARE_DELETE |
win32file.FILE_SHARE_READ |
win32file.FILE_SHARE_WRITE,
None,
win32file.OPEN_EXISTING,
0,
None)
file_descriptor = msvcrt.open_osfhandle(
handle, os.O_TEXT | os.O_RDONLY)
fh = open(file_descriptor, encoding='utf-8',
errors='ignore', newline="\n")
self.reopen_check = "time"
self.fh = fh
self.file = file
# Uncommenting this code demonstrate that there's no problem reading the file!!!!
# -------------------------------------------------------------------------------
# line = None
# self.wait_count = 0
# while not line:
# line = self.fh.readline()
def __iter__(self):
return self
def __next__(self):
line = None
self.wait_count = 0
while not line:
line = self.fh.readline()
return line
# ##############################
# ENTRY POINT
# ##############################
if __name__ == "__main__":
my_file = FileTail('C:\LOGS\DANNI.WEB\PROVA.LOG')
for line in my_file:
print(line)
Now, if you try to execute this script, you will receive this error message:
Traceback (most recent call last):
File "C:\Users\me\Desktop\prova.py", line 63, in <module>
for line in my_file:
File "C:\Users\me\Desktop\prova.py", line 53, in __next__
line = self.fh.readline()
OSError: [Errno 9] Bad file descriptor
If I uncomment the commented code in the "open" method I can read the whole file, so I don't think the problem is in the usage of the win32 api to open the file... so... what I'm missing?
Why using the iterator protocol I get the error message? Is it a thread related problem? How can I fix it?
I know that there will be probably a thousand of work-around but I want to understand why this code is not working...
Thank you all for the help you will provide and sorry for my very bad english... :(
Dave