I am trying to execute this program taken from the online python documentation(https://docs.python.org/3.6/library/mmap.html) and its about the implementation of mmap.
import mmap
# write a simple example file
with open("hello.txt", "wb") as f:
f.write(b"Hello Python!\n")
with open("hello.txt", "r+b") as f:
# memory-map the file, size 0 means whole file
mm = mmap.mmap(f.fileno(), 0)
# read content via standard file methods
print(mm.readline()) # prints b"Hello Python!\n"
# read content via slice notation
print(mm[:5]) # prints b"Hello"
# update content using slice notation;
# note that new content must have same size
mm[6:] = b" world!\n"
# ... and read again using standard file methods
mm.seek(0)
print(mm.readline()) # prints b"Hello world!\n"
# close the map
mm.close()
Unfortunately, on my system when I execute on command line I get these errors.
user@logger$ python3 mmap1.py
Traceback (most recent call last):
File "mmap1.py", line 9, in <module>
mm = mmap.mmap(f.fileno(), 0)
OSError: [Errno 22] Invalid argument
user@logger$ python mmap1.py
Traceback (most recent call last):
File "mmap1.py", line 9, in <module>
mm = mmap.mmap(f.fileno(), 0)
mmap.error: [Errno 22] Invalid argument
user@logger$ which python
/usr/bin/python
user@logger$ python --version
Python 2.7.15rc1
user@logger$ python3 --version
Python 3.6.7
About my system, Ubuntu Linux on Virtual machine - VirtualBox:
user@logger$ uname -a
Linux user-vm 4.15.0-42-generic #45-Ubuntu SMP Thu Nov 15 19:32:57 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
On windows, using PyCharm I am able to get this program executed without any issues. On a virtual machine Linux, however, I get these errors. What am I missing.
Thank you.