Problem using lseek

John Machin sjmachin at lexicon.net
Wed Feb 5 16:04:34 EST 2003


Michael Mayhew <mayhew at cis.ohio-state.edu> wrote in message news:<3E4110E8.3060206 at cis.ohio-state.edu>...
> Greetings,
> 
> 	I have been using Python faithfully for the past five months and am quite 
> happy having learned it. That being said, I am currently having a real 
> honey of a problem. I am trying to use the os module's lseek function to 
> set a file descriptor to a new offset position in a file. However, when 
> running the code through the interpreter, I continue receiving a 
> TypeError message:
> 
> Traceback (most recent call last):
>    File "testseq.py", line 18, in ?
>      os.lseek(somefile, someoffset, 1)
> TypeError: an integer is required
> 
> I have already checked the type of someoffset and it is indeed an 
> integer. I have also read the library reference to make sure that no new 
> arguments to the function have been created. What else is needed for 
> this statement to function correctly, as I am passing a file descriptor, 
> an integer, and an integer, as the library reference requires. TIA.

It would be great if you showed the whole of testseq.py, or at least
the result of

print type(somefile), type(someoffset)

but I'm betting that you opened the file with built-in open(), not
with os.open() --- an os module fd is a low-level Unix-style fd which
is an *integer*; 0 is stdin, 1 is stdout, 2 is stderr, 3 is your first
file opened etc --- and the exception is in respect of your *first*
argument.

>>> import os
>>> myfd = os.open("\\config.sys", os.O_RDONLY)
>>> myfd
3
>>> print type(myfd)
<type 'int'>
>>>

Either:
   hi = open("foo", "rb") followed by hi.seek(.....)
or:
   lo = os.open("foo",...) followed by os.lseek(lo, .....)

You should only be using the os module if you want to do some
nitty-gritty function that's not supported by the built-in file type
(IMHO).

HTH,
John




More information about the Python-list mailing list