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

Discussion in 'Python' started by krishna2prasad@gmail.com, May 14, 2013.

  1. Guest

    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!
     
    , May 14, 2013
    #1
    1. Advertising

  2. Guest

    On Tue, May 14, 2013, at 03:00 PM, 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
     
    , May 14, 2013
    #2
    1. Advertising

  3. Roy Smith Guest

    In article <>,
    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.
     
    Roy Smith, May 14, 2013
    #3
  4. On 14.05.2013 21:00, 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
     
    Andreas Perstinger, May 14, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. venkat

    regarding lseek and fread

    venkat, May 27, 2007, in forum: C Programming
    Replies:
    2
    Views:
    449
    Martin Ambuhl
    May 27, 2007
  2. golden

    lseek and write question

    golden, Nov 16, 2007, in forum: C++
    Replies:
    3
    Views:
    593
    James Kanze
    Nov 17, 2007
  3. Gordon Beaton
    Replies:
    3
    Views:
    1,391
  4. smarty

    illegal seek

    smarty, May 14, 2008, in forum: C Programming
    Replies:
    16
    Views:
    5,079
    Keith Thompson
    May 16, 2008
  5. pavunkumar

    Lseek Error

    pavunkumar, Mar 31, 2009, in forum: C Programming
    Replies:
    2
    Views:
    490
    Rob Clarke
    Mar 31, 2009
Loading...

Share This Page