Standard Module posixfile

posixfile posixfile object

Note: This module will become obsolete in a future release. The locking operation that it provides is done better and more portably by the fcntl.lockf() call.

This module implements some additional functionality over the built-in file objects. In particular, it implements file locking, control over the file flags, and an easy interface to duplicate the file object. The module defines a new file object, the posixfile object. It has all the standard file object methods and adds the methods described below. This module only works for certain flavors of , since it uses fcntl() for file locking.

To instantiate a posixfile object, use the open() function in the posixfile module. The resulting object looks and feels roughly the same as a standard file object.

The posixfile module defines the following constants:


\begin{datadesc}{SEEK_SET}
offset is calculated from the start of the file
\end{datadesc}


\begin{datadesc}{SEEK_CUR}
offset is calculated from the current position in the file
\end{datadesc}


\begin{datadesc}{SEEK_END}
offset is calculated from the end of the file
\end{datadesc}

The posixfile module defines the following functions:


\begin{funcdesc}{open}{filename\optional{\, mode\optional{\, bufsize}}}
Create ...
...terpreted the same way as by the built-in \code{open()} function.
\end{funcdesc}


\begin{funcdesc}{fileopen}{fileobject}
Create a new posixfile object with the g...
...ject has the same filename and mode as the original
file object.
\end{funcdesc}

The posixfile object defines the following additional methods:


\begin{funcdesc}{lock}{fmt\, \optional{len\optional{\, start
\optional{\, whence...
...out the arguments refer to the fcntl
manual page on your system.
\end{funcdesc}


\begin{funcdesc}{flags}{\optional{flags}}
Set the specified flags for the file ...
...n about the flags
refer to the fcntl manual page on your system.
\end{funcdesc}


\begin{funcdesc}{dup}{}
Duplicate the file object and the underlying file point...
...riptor. The resulting object behaves as if it were newly
opened.
\end{funcdesc}


\begin{funcdesc}{dup2}{fd}
Duplicate the file object and the underlying file po...
...therwise the resulting object behaves as if it were newly opened.
\end{funcdesc}


\begin{funcdesc}{file}{}
Return the standard file object that the posixfile obj...
...s necessary for functions that insist on a
standard file object.
\end{funcdesc}

All methods return IOError when the request fails.

Format characters for the lock() method have the following meaning:


\begin{tableiii}{\vert c\vert l\vert c\vert}{samp}{Format}{Meaning}{}
\lineiii{...
...{}
\lineiii{w}{request a write lock for the specified section}{}
\end{tableiii}

In addition the following modifiers can be added to the format:


\begin{tableiii}{\vert c\vert l\vert c\vert}{samp}{Modifier}{Meaning}{Notes}
\l...
...e requested lock, or
\code{None} if there is no conflict.}{(1)}
\end{tableiii}

Note:

(1) The lock returned is in the format (mode, len, start, whence, pid) where mode is a character representing the type of lock ('r' or 'w'). This modifier prevents a request from being granted; it is for query purposes only.

Format character for the flags() method have the following meaning:


\begin{tableiii}{\vert c\vert l\vert c\vert}{samp}{Format}{Meaning}{}
\lineiii{...
...called non-blocking flag)}{}
\lineiii{s}{synchronization flag}{}
\end{tableiii}

In addition the following modifiers can be added to the format:


\begin{tableiii}{\vert c\vert l\vert c\vert}{samp}{Modifier}{Meaning}{Notes}
\l...
... in which the characters represent the flags that
are set.}{(2)}
\end{tableiii}

Note:

(1) The ! and = modifiers are mutually exclusive.

(2) This string represents the flags after they may have been altered by the same call.

Examples:

from posixfile import *

file = open('/tmp/test', 'w')
file.lock('w|')
...
file.lock('u')
file.close()