Python 2.7 on Windows. Trying to use mmap module, but use open file handler instead of with open(filename, "r+b") as f:
I just open it and get an WindowsError [Error 5]
.
It does reproduce, either run as Administrator or not.
Using codecs.open()
doesn't resolve the problem.
# -*- coding: utf8 -*-
from __future__ import print_function
import mmap
class QSHFile(object):
def __init__(self, filename):
self.filename = filename
self.file = open(filename, 'r')
self.fileno = self.file.fileno()
self.mm = mmap.mmap(self.fileno, 0)
print(self.mm[:5]) # prints first 5
if __name__ == '__main__':
qsh = QSHFile('example.qsh')
After a bit or research, I came into:
#! /usr/bin/python
# -*- coding: utf8 -*-
from __future__ import print_function
from mmap import ACCESS_READ, mmap
class QSHFile(object):
def __init__(self, filename):
self.filename = filename
self.file = open(filename, 'rb')
self.fileno = self.file.fileno()
self.mm = mmap(self.fileno, 0, access=ACCESS_READ)
print(self.mm[:5])
if __name__ == '__main__':
qsh = QSHFile('example.qsh')
And now it's working fine. Am I doing correct now?
Yes, that's it, thanks to everyone!
# -*- coding: utf8 -*-
from __future__ import print_function
from mmap import ACCESS_READ, mmap
from binascii import hexlify as hex
class QSHFile(object):
def __init__(self, filename):
self.filename = filename
self.file = open(filename, 'rb')
print('File [%s] opened' % self.filename)
self.fileno = self.file.fileno()
self.mm = mmap(self.fileno, 0, access=ACCESS_READ)
print('File size: %s bytes' % self.mm.size())
print(hex(self.mm[:5]))
if __name__ == '__main__':
qsh = QSHFile('example.qsh')