BytesIO like file object

EltraEden

I can't understand the difference of these two BytesIO objects. If i do this :

f = open('decoder/logs/testfile.txt', 'rb')
file = io.BytesIO(f.read())
decode(file,0)

then in decode method this works :

for line in islice(file, lines, None):

But if i create BytesIO like this :

file = io.BytesIO()
file.write(b"Some codded message")
decode(file, 0)

Then loop in decode method returns nothing. What i understand is BytesIO should act as file like object but stored in memory. So why when i try to pass only one line of file this loop return nothing like there was no lines in file?

Plaváček

The difference is the current position in the stream. In the first example, the position is at the beginning. But in the second example, it is at the end. You can obtain the current position using file.tell() and return back to the start by file.seek(0):

import io
from itertools import islice


def decode(file, lines):
   for line in islice(file, lines, None):
      print(line)


f = open('testfile.txt', 'rb')
file = io.BytesIO(f.read())
print(file.tell())  # The position is 0
decode(file, 0)


file = io.BytesIO()
file.write(b"Some codded message")
print(file.tell())  # The position is 19
decode(file, 0)

file = io.BytesIO()
file.write(b"Some codded message")
file.seek(0)
print(file.tell())  # The position is 0
decode(file, 0)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Writing a BytesIO object to a file, 'efficiently'

Pandas read_json() encoding = 'utf-8-sig' option is not working for BytesIO object (file-like object)

Convert file into BytesIO object using python

TypeError: a bytes-like object is required, not 'str' Using BytesIO

csv file convert to io.BytesIO object, then stream to blob storage ,meets value type error:a bytes-like object is required, not '_io.TextIOWrapper'

What is correct way to load json file stored in the BytesIO object?

Bokeh - Pandas not able to read bytesIO object of excel file from JS

BytesIO object to image

How do i convert from '_io.BytesIO' to a bytes-like object in python3.6?

Convert from '_io.BytesIO' to a bytes-like object in python3.6?

Convert BytesIO into File

How to differentiate a file like object from a file path like object

Open a file (of type BytesIO) with the function

Type hint for a file or file-like object?

Python error: PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x1144e9860>

UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000002154C6AE400>

Assertion error for BytesIO object in Python unittest

convert html file to BytesIO then pass as Flask variable

Minimal implementation of a "file-like" object?

Create a gzip file like object for unit testing

attach file like object to email python 3

Check if object is file-like in Python

Create file-like object for data buffer

Converting Tree Like File Directory to a JSON object

Passing a file-like object to write() method of another file-like object

Python CSV file to bytes or seekable file-like object

Why I have to convert bytes from BytesIO then convert back to BytesIO so it can be read as PDF file response?

OSError: cannot identify image file <_io.BytesIO object at 0x00000198001B9E08> while trying to get images from s3 and open image

Django with PIL - '_io.BytesIO' object has no attribute 'name'

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive