Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Illegal seek error with seek() and os.lseek()

Reply
Thread Tools

Illegal seek error with seek() and os.lseek()

 
 
krishna2prasad@gmail.com
Guest
Posts: n/a
 
      05-14-2013
I am trying to use os.open() and os.lseek() methods to operate on a device file in Linux. My code goes something like this -

# first, open the file as a plain binary
try:
self.file = open(/dev/relpcfpga, "r+b", buffering=0)

except IOError:
raise IOError ('Failed to open.')

# Figure out file size
self.file.seek(0, 2)
self.file_size = self.file.tell()


The method seek() complains "OSError: [Errno 29] Illegal seek"
The device relpcfpga is a char device.

The same code works with a normal text file.
I have tried to use os.open() and os.lseek() methods, but see the same error.
Is there a different method to operate on device files?


Thanks!
 
Reply With Quote
 
 
 
 
marduk@python.net
Guest
Posts: n/a
 
      05-14-2013
On Tue, May 14, 2013, at 03:00 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I am trying to use os.open() and os.lseek() methods to operate on a
> device file in Linux. My code goes something like this -
>
> # first, open the file as a plain binary
> try:
> self.file = open(/dev/relpcfpga, "r+b", buffering=0)
>
> except IOError:
> raise IOError ('Failed to open.')
>
> # Figure out file size
> self.file.seek(0, 2)
> self.file_size = self.file.tell()
>
>
> The method seek() complains "OSError: [Errno 29] Illegal seek"
> The device relpcfpga is a char device.
>
> The same code works with a normal text file.
> I have tried to use os.open() and os.lseek() methods, but see the same
> error.
> Is there a different method to operate on device files?


Some file streams are not seekable. Specifically, some (all?) char
devices aren't seekable (because e.g. they can't be rewound or they have
no end). You'd get the same error in C (well it would return -1).

See also: http://www.linuxintro.org/wiki/Device
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      05-14-2013
In article <(E-Mail Removed)>,
(E-Mail Removed) wrote:

> I am trying to use os.open() and os.lseek() methods to operate on a device
> file in Linux. My code goes something like this -
>
> # first, open the file as a plain binary
> try:
> self.file = open(/dev/relpcfpga, "r+b", buffering=0)
>
> except IOError:
> raise IOError ('Failed to open.')
>
> # Figure out file size
> self.file.seek(0, 2)
> self.file_size = self.file.tell()
>
>
> The method seek() complains "OSError: [Errno 29] Illegal seek"
> The device relpcfpga is a char device.
>
> The same code works with a normal text file.
> I have tried to use os.open() and os.lseek() methods, but see the same error.
> Is there a different method to operate on device files?


In general, seek() works on special files, when it makes sense. But,
the "in general" part is critical. Not all devices support the seek
operation. I have no idea what /dev/relpcfpga is (a google search for
relpcfpga came up with exactly one hit -- your post!) so I can't tell
you if it supports seek() or not.
 
Reply With Quote
 
Andreas Perstinger
Guest
Posts: n/a
 
      05-14-2013
On 14.05.2013 21:00, (E-Mail Removed) wrote:
> # first, open the file as a plain binary
> try:
> self.file = open(/dev/relpcfpga, "r+b", buffering=0)


Aren't you missing the quotes for "/dev/relpcfpga"?

> The method seek() complains "OSError: [Errno 29] Illegal seek"
> The device relpcfpga is a char device.


Are you sure that your device is seekable?
Try

f = open("/dev/relpcfpga", "r+b", buffering=0)
print(f.seekable())

Bye, Andreas
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with printing a bit pattern with printf and %x matt.jaffe@gmail.com C Programming 9 04-19-2013 12:14 PM
Illegal seek with os.popen pruebauno@latinmail.com Python 4 06-04-2009 09:28 PM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net Building Controls 0 07-29-2008 01:37 AM
illegal seek smarty C Programming 16 05-16-2008 07:39 PM



Advertisments