LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-31-2015, 02:48 AM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Rep: Reputation: 30
python read from byte x to byte y


Hello all,

I have written some code to read bytes from a file one chunk at a time, but I would like to start and end at a particular position, e.g. bytes_from_file(self, chunksize, startPos, endPos), is this possible in python, if so how can it be achieved?


Code:
def bytes_from_file(self, chunksize=8192):
  self.log.message("setting bytes_from_file to chunksize: " + str(chunksize))
  file = open(self.File, "rb")
  
  try:
    bytes_read = file.read(chunksize)
    
    while bytes_read:
      for b in bytes_read:
        yield(b) 
      bytes_read = file.read(chunksize)
  
  finally:
    self.log.message("closing file")
    file.close()
Many thanks,
trscookie
 
Old 01-31-2015, 03:08 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
yes, definitely possible, but the function you posted has only to parameters. looks like the first step on your road. Do you know what will that yield command do? How do you want to specify start and end position?
 
Old 01-31-2015, 03:24 AM   #3
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
Currently I am using it like so:

Code:
for b in f.bytes_from_file(1024):
  print b

I'm thinking that I will probably want to use fseek to read from byte 'y' and exit return after x amount of chunks, something like:

Code:
for b in f.bytes_from_file(1024, 0, 1024 * 10):
  print b

Code:
def bytes_from_file(self, chunksize=8192, posX, posY):
  self.log.message("setting bytes_from_file to chunksize: " + str(chunksize))
  file = open(self.File, "rb")
  
  try:
    file.seek(posX, 0)
    
    bytes_read = file.read(chunksize)
    
    while bytes_read < posY:
      for b in bytes_read:
        yield(b) 
      bytes_read = file.read(chunksize)
  
  finally:
    self.log.message("closing file")
    file.close()

Last edited by trscookie; 01-31-2015 at 03:26 AM.
 
Old 01-31-2015, 03:38 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
that looks much better. But I think you need to calculate the length of the result and count the bytes found (the while condition you wrote will not work)
 
Old 01-31-2015, 04:09 AM   #5
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
Hum, think I am nearly there, just not quite sure what I am missing:



Code:
def bytes_from_file(self, chunksize=8192, posX=0, count=1):
  self.log.message("file size: " + str(self.ByteSize))
  self.log.message("setting bytes_from_file to chunksize: " + str(chunksize) + " pos: " + str(posX) + " count: " + str(count * chunksize))
  file = open(self.File, "rb")
  
  try:
    file.seek(posX, 0)
    
    bytes_read = file.read(chunksize)
    
    while bytes_read < (chunksize * count):
      for b in bytes_read:
        yield(b)
      bytes_read = file.read(chunksize)
      
  finally:
    self.log.message("closing file")
    file.close()

Code:
 f = bsFile(log, myfile)
 
 if f.exists():
   for b in f.bytes_from_file(1024, 0, 1):
     print b

Log:
Code:
file size: 1631
setting bytes_from_file to chunksize: 1024 pos: 0 count: 1024
closing file


I can get the filesize by using self.bytesize but I should only be fetching the first 1024 bytes :S

Cheers again
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] memcpy fails to copy data, but byte by byte assignment work venu_s Programming 7 07-08-2011 03:29 AM
Need to copy an entire disk byte-for-byte Pawprint Linux - Software 6 06-16-2011 11:01 AM
byte to byte remote comparison louisJ Linux - Newbie 3 09-21-2007 05:28 PM
backup byte-for-byte axion0917 Linux - Software 2 12-11-2003 05:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration