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

marduk at python.net marduk at python.net
Tue May 14 16:00:44 EDT 2013


On Tue, May 14, 2013, at 03:00 PM, krishna2prasad at gmail.com 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



More information about the Python-list mailing list