Hướng dẫn python in-memory file with name - tệp trong bộ nhớ python có tên

Tôi biết tôi có thể sử dụng io.StringIO hoặc io.BytesIO để trả về một tay cầm tệp mở có thể được ghi và đọc từ đó.

Tuy nhiên, tôi đang tìm kiếm một cách để làm cho một vùng trong bộ nhớ trông giống như một tệp đĩa được đặt tên chưa được mở.

Lý do tôi muốn điều này là do có những thói quen lấy tên của một tệp đĩa làm đối số và các thói quen này sau đó mở tệp và thao tác nó. Trong một số trường hợp, tôi muốn đầu vào hoặc đầu ra cho các thói quen này là bộ đệm bộ nhớ, không phải là tệp đĩa và đối với các thói quen đó, tôi không thể vượt qua xử lý tệp đã mở.

Ví dụ: một thói quen như vậy là Image.save() từ PIL, mong đợi một tên đường dẫn là đối số của nó, không phải là một tay cầm tệp đã mở. Khi sử dụng thói quen đó, tôi muốn dữ liệu hình ảnh được lưu trực tiếp vào bộ đệm bộ nhớ mà không có bất kỳ tệp trung gian nào được thực hiện. Ngoài ra còn có nhiều thói quen khác lấy tên đường dẫn làm đối số mà tôi muốn hành vi này.

Có cách nào để thực hiện điều này trong Python không?

Các phần khác của tài liệu này đã giải thích cách Rasterio có thể truy cập dữ liệu được lưu trữ trong các tệp hiện có trên đĩa được viết bởi các chương trình khác hoặc ghi các tệp được sử dụng bởi các chương trình GIS khác. Tên tệp là các đầu vào và tệp điển hình trên đĩa là các đầu ra điển hình.

with rasterio.open('example.tif') as dataset:
    data_array = dataset.read()

Có các tùy chọn khác nhau cho các chương trình Python có các luồng byte, ví dụ: từ ổ cắm mạng, làm đầu vào hoặc đầu ra của chúng thay vì tên tệp. Một là việc sử dụng một tệp tạm thời trên đĩa.

import tempfile


with tempfile.NamedTemporaryFile() as tmpfile:
    tmpfile.write(data)
    with rasterio.open(tmpfile.name) as dataset:
        data_array = dataset.read()

Một cách khác là Rasterio sườn ____10, một sự trừu tượng hóa cho các đối tượng trong hệ thống tập tin trong bộ nhớ của GDAL.

MemoryFile: Bytesio đáp ứng được đặt tên

Lớp

import tempfile


with tempfile.NamedTemporaryFile() as tmpfile:
    tmpfile.write(data)
    with rasterio.open(tmpfile.name) as dataset:
        data_array = dataset.read()
0 hoạt động hơi giống
import tempfile


with tempfile.NamedTemporaryFile() as tmpfile:
    tmpfile.write(data)
    with rasterio.open(tmpfile.name) as dataset:
        data_array = dataset.read()
2 và
import tempfile


with tempfile.NamedTemporaryFile() as tmpfile:
    tmpfile.write(data)
    with rasterio.open(tmpfile.name) as dataset:
        data_array = dataset.read()
3. Một tệp GeoTiff trong một chuỗi các byte
import tempfile


with tempfile.NamedTemporaryFile() as tmpfile:
    tmpfile.write(data)
    with rasterio.open(tmpfile.name) as dataset:
        data_array = dataset.read()
4 có thể được mở trong bộ nhớ như được hiển thị bên dưới.

from rasterio.io import MemoryFile


 with MemoryFile(data) as memfile:
     with memfile.open() as dataset:
         data_array = dataset.read()

Mã này có thể nhanh hơn nhiều lần so với mã sử dụng

import tempfile


with tempfile.NamedTemporaryFile() as tmpfile:
    tmpfile.write(data)
    with rasterio.open(tmpfile.name) as dataset:
        data_array = dataset.read()
3 với giá gấp đôi giá trong bộ nhớ.

Viết bộ nhớ 

Gia tăng ghi vào một

import tempfile


with tempfile.NamedTemporaryFile() as tmpfile:
    tmpfile.write(data)
    with rasterio.open(tmpfile.name) as dataset:
        data_array = dataset.read()
0 trống cũng có thể.

with MemoryFile() as memfile:
    while True:
        data = f.read(8192)  # ``f`` is an input stream.
        if not data:
            break
        memfile.write(data)
    with memfile.open() as dataset:
        data_array = dataset.read()

Hai chế độ này không tương thích: A

import tempfile


with tempfile.NamedTemporaryFile() as tmpfile:
    tmpfile.write(data)
    with rasterio.open(tmpfile.name) as dataset:
        data_array = dataset.read()
0 được khởi tạo với chuỗi byte không thể được mở rộng.

Một

import tempfile


with tempfile.NamedTemporaryFile() as tmpfile:
    tmpfile.write(data)
    with rasterio.open(tmpfile.name) as dataset:
        data_array = dataset.read()
0 trống cũng có thể được viết để sử dụng các phương thức API dữ liệu.

with MemoryFile() as memfile:
    with memfile.open(driver='GTiff', count=3, ...) as dataset:
        dataset.write(data_array)

Đọc MemoryFiles

Giống như

import tempfile


with tempfile.NamedTemporaryFile() as tmpfile:
    tmpfile.write(data)
    with rasterio.open(tmpfile.name) as dataset:
        data_array = dataset.read()
2,
import tempfile


with tempfile.NamedTemporaryFile() as tmpfile:
    tmpfile.write(data)
    with rasterio.open(tmpfile.name) as dataset:
        data_array = dataset.read()
0 thực hiện giao thức tệp Python và cung cấp các phương thức
from rasterio.io import MemoryFile


 with MemoryFile(data) as memfile:
     with memfile.open() as dataset:
         data_array = dataset.read()
1,
from rasterio.io import MemoryFile


 with MemoryFile(data) as memfile:
     with memfile.open() as dataset:
         data_array = dataset.read()
2 và
from rasterio.io import MemoryFile


 with MemoryFile(data) as memfile:
     with memfile.open() as dataset:
         data_array = dataset.read()
3. Do đó, các trường hợp phù hợp như các đối số cho các phương thức như yêu cầu.post ().

with MemoryFile() as memfile:
    with memfile.open(driver='GTiff', count=3, ...) as dataset:
        dataset.write(data_array)

     requests.post('https://example.com/upload', data=memfile)

Python có thể viết vào bộ nhớ không?

Viết một tệp ánh xạ bộ nhớ bằng MMAP của Python. Ánh xạ bộ nhớ là hữu ích nhất để đọc các tệp, nhưng bạn cũng có thể sử dụng nó để ghi các tệp.you can also use it to write files.

Tệp IO Python là gì?

Mở một tập tin để nối thêm ở định dạng nhị phân.Con trỏ tệp ở cuối tệp nếu tệp tồn tại.Đó là, tệp ở chế độ phụ lục.Nếu tệp không tồn tại, nó sẽ tạo một tệp mới để viết.. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

Luồng tệp trong Python là gì?

Đối tượng tệp là luồng dữ liệu hỗ trợ phương thức tiếp theo () để đọc từng dòng tệp.Khi kết thúc của tập tin gặp phải, ngoại lệ dừng lại được nâng lên.f = open ("python.txt", "r") trong khi true: thử: line = next (f) in (line, end = "") ngoại trừ stopitation: break f.close ()a data stream that supports next() method to read file line by line. When end of file is encountered, StopIteration exception is raised. f=open("python.txt","r") while True: try: line=next(f) print (line, end="") except StopIteration: break f.close()

Một tệp như đối tượng trong Python là gì?

Đối tượng tệp còn được gọi là các đối tượng hoặc luồng giống như tệp.Thực tế, có ba loại đối tượng tệp: tệp nhị phân thô, tệp nhị phân được đệm và tệp văn bản.Giao diện của chúng được xác định trong mô -đun IO.Cách kinh điển để tạo một đối tượng tệp là bằng cách sử dụng hàm Open ().. There are actually three categories of file objects: raw binary files, buffered binary files and text files. Their interfaces are defined in the io module. The canonical way to create a file object is by using the open() function.