| Python Library Reference |
Availability:Unix.
This module implements some additional functionality over the built-infile objects. In particular, it implements file locking, control overthe file flags, and an easy interface to duplicate the file object.The module defines a new file object, the posixfile object. Ithas all the standard file object methods and adds the methodsdescribed below. This module only works for certain flavors ofUnix, since it usesfcntl.fcntl() for file locking.
To instantiate a posixfile object, use theopen() functionin theposixfile module. The resulting object looks andfeels roughly the same as a standard file object.
Theposixfile module defines the following constants:
Theposixfile module defines the following functions:
The posixfile object defines the following additional methods:
0.start specifies the starting offset of the section, where the default is0. Thewhence argument specifies where the offset is relative to. It accepts one of the constantsSEEK_SET,SEEK_CUR orSEEK_END. The default isSEEK_SET. For more information about the arguments refer to thefcntl(2) manual page on your system.All methods raiseIOError when the request fails.
Format characters for thelock() method have the followingmeaning:
| Format | Meaning |
|---|---|
| u | unlock the specified region |
| r | request a read lock for the specified section |
| w | request a write lock for the specified section |
In addition the following modifiers can be added to the format:
| Modifier | Meaning | Notes |
|---|---|---|
| | | wait until the lock has been granted | |
| ? | return the first lock conflicting with the requested lock, orNone if there is no conflict. | (1) |
Note:
(mode,len,start,whence,pid) wheremode is a characterrepresenting the type of lock ('r' or 'w'). This modifier prevents arequest from being granted; it is for query purposes only.Format characters for theflags() method have the followingmeanings:
| Format | Meaning |
|---|---|
| a | append only flag |
| c | close on exec flag |
| n | no delay flag (also called non-blocking flag) |
| s | synchronization flag |
In addition the following modifiers can be added to the format:
| Modifier | Meaning | Notes |
|---|---|---|
| ! | turn the specified flags 'off', instead of the default 'on' | (1) |
| = | replace the flags, instead of the default 'OR' operation | (1) |
| ? | return a string in which the characters represent the flags that are set. | (2) |
Notes:
Examples:
import posixfilefile = posixfile.open('/tmp/test', 'w')file.lock('w|')...file.lock('u')file.close()| Python Library Reference |