Python standard library base mmap: memory-mapped files

Source: Internet
Author: User
Tags rewind

#作用: Create a memory-mapped file instead of reading the content directly
Text message content: as follows (name is Text.txt)

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec

Egestas, enim et consectetuer ullamcorper, lectus ligula rutrum Leo,

A elementum elit tortor EU quam. Duis tincidunt nisi ut ante. Nulla

Facilisi. Sed tristique eros eu libero. Pellentesque vel

Arcu. Vivamus purus Orci, iaculis ac, suscipit sit amet, pulvinar EU,

Lacus. Praesent placerat tortor sed nisl. Nunc blandit diam Egestas

Dui. Pellentesque habitant morbi tristique senectus et netus et

Malesuada fames ac turpis egestas. Aliquam Viverra Fringilla

Leo. Nulla feugiat augue eleifend Nulla. Vivamus Mauris. Vivamus SED

Mauris in Nibh placerat egestas. Suspendisse Potenti. Mauris

Massa. Ut eget velit auctor tortor blandit Sollicitudin. Suspendisse

Imperdiet Justo.

Brief introduction

Memory-mapped file objects resemble strings and objects that resemble files. Unlike the usual string object, it can be mutable. You can use a Mmap object when you need a string, such as using the RE module to search for a memory-mapped file. Modify a single character: obj[index] = ' A ', or change the string by slicing: [i1:i2] = ' ... '. The current file location data can be read, and Seek () is positioned to another location in the file.

Memory-mapped files are created by the Mmap constructor, which differs in UNIX and Windows. Both need to provide a file descriptor. If you want to map an open Python file object, you need to use Fileno (). Or, use the Os.open () function (to return a file descriptor, but if you need to close it manually) to open the file. To create a writable memory-mapped file for buffer, you must first flush () the file to ensure that local modifications to the buffer are also in effect for the mapping.

Both UNIX and Windows versions of the construction method can specify optional parameters for access. Options: Access_read,access_write, or access_copy, respectively read, write, copy access. Windows default is write,. The initial memory value is specified by the file, and the object that writes Access_read will be BAOC. The allocation to the Access_read memory map throws a TypeError exception. Access_write can affect memory and underlying files. Access_copy affects memory, but does not update the underlying file.

#注意: All of these modules are 2. x version of, 3.X may not have these modules

#注意: The mmap () parameter and behavior under UNIX and Windows are different, please see the official documentation at the following address: https://docs.python.org/2/library/mmap.html

#读取文件
"""
You can use the mmap () function to create an intrinsic mapping file, the first parameter is the file descriptor, the Fileno () method of the Files object, or from the Os.open ()
。 The call is responsible for opening the file before calling Mmap () and is responsible for closing the file when it is no longer needed
The second parameter is the size (in bytes) of the portion of the file to be mapped, which is 0, the entire file is mapped, and if the size is larger than the file's current size, the file.
"""
#注意: Creating a 0-length mapping is not supported under Windows
#此2平台都支持一个可选关键字参数access, Access_read,access_write, or access_copy, respectively, read, write, copy access. Windows default is Write,
#对内存的赋值不会写至文件
Import Contextlib,mmap,re
With open (R ' Text.txt ', ' R ') as F:
With Contextlib.closing (Mmap.mmap (F.fileno (), 0,access=mmap. Access_read)) as M:
print ' first red: ', M.read (10)
print ' first slice: ', m[:10]
print ' 2nd: ', M.read (10)

#文件指定会跟踪通过一个分片操作访问最后一个字节

#写入
Import Shutil
Shutil.copyfile (' Text.txt ', ' lorem_copy.txt ')

Word = ' consectetuer '
reversed = Word[::-1]
print ' Looking for: ', word
print ' Replacing with: ', reversed

With open (' Lorem_copy.txt ', ' r+ ') as F:
With Contextlib.closing (Mmap.mmap (F.fileno (), 0)) as M:
print ' Before: '
Print M.readline (). Rstrip ()
M.seek (0) # Rewind

loc = M.find (Word)
M[loc:loc+len (word)] = reversed
M.flush ()

M.seek (0) # Rewind
print ' After: '
Print M.readline (). Rstrip ()

F.seek (0) # Rewind
print ' File: '
Print F.readline (). Rstrip ()
#内存和文件中第一行中间的单词consectetuer将被替换

#复制模式: Using access_copy does not change the files that are actually stored
print ' Copy ' *20
Shutil.copyfile (' Lorem.txt ', ' lorem_copy.txt ')

Word = ' consectetuer '
reversed = Word[::-1]

With open (' Lorem_copy.txt ', ' r+ ') as F:
With Contextlib.closing (Mmap.mmap (F.fileno (), 0,
Access=mmap. Access_copy)
) as M:
print ' Memory before: '
Print M.readline (). Rstrip ()
print ' File before: '
Print F.readline (). Rstrip ()
Print

M.seek (0) # Rewind
loc = M.find (Word)
M[loc:loc+len (word)] = reversed

M.seek (0) # Rewind
print ' Memory after: '
Print M.readline (). Rstrip ()

F.seek (0)
print ' File after: '
Print F.readline (). Rstrip ()

#正则表达式
#由于内存映射文件就类似于一个字符串, so it also applies to other processing string modules, such as regular
print ' re ' *20
Pattern = Re.compile (R ' (\.\w+)? ( [^.]? nulla[^.] *?\.)‘,
Re. Dotall | Re. IGNORECASE | Re. MULTILINE)

With open (' Lorem.txt ', ' R ') as F:
With Contextlib.closing (Mmap.mmap (F.fileno (), 0,
Access=mmap. Access_read)
) as M:
For match in Pattern.findall (m):
Print match[1].replace (' \ n ', ')
#contextlib: Use the closing () function to create a context manager for a memory-mapped file

Python standard library base mmap: memory-mapped files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.