path, mode) |
Note: Using access() to check if a user is authorized to e.g. open a file before actually doing so using open() creates a security hole, because the user might exploit the short time interval between checking and opening the file to manipulate it.
path) |
fd) |
) |
) |
path) |
path, mode) |
S_ISUID
S_ISGID
S_ENFMT
S_ISVTX
S_IREAD
S_IWRITE
S_IEXEC
S_IRWXU
S_IRUSR
S_IWUSR
S_IXUSR
S_IRWXG
S_IRGRP
S_IWGRP
S_IXGRP
S_IRWXO
S_IROTH
S_IWOTH
S_IXOTH
Note:
Although Windows supports chmod(), you can only
set the file's read-only flag with it (via the S_IWRITE
and S_IREAD
constants or a corresponding integer value).
All other bits are ignored.
path, uid, gid) |
path, uid, gid) |
src, dst) |
path) |
'.'
and '..'
even if they are present in the
directory.
Availability: Macintosh, Unix, Windows.
Changed in version 2.3: On Windows NT/2k/XP and Unix, if path is a Unicode object, the result will be a list of Unicode objects..
path) |
path[, mode]) |
0666
(octal). The current
umask value is first masked out from the mode.
Availability: Macintosh, Unix.
FIFOs are pipes that can be accessed like regular files. FIFOs exist until they are deleted (for example with os.unlink()). Generally, FIFOs are used as rendezvous between ``client'' and ``server'' type processes: the server opens the FIFO for reading, and the client opens it for writing. Note that mkfifo() doesn't open the FIFO -- it just creates the rendezvous point.
path[, mode=0600, device]) |
device) |
device) |
major, minor) |
path[, mode]) |
0777
(octal). On some systems,
mode is ignored. Where it is used, the current umask value is
first masked out.
Availability: Macintosh, Unix, Windows.
path[, mode]) |
0777
(octal). This function does not properly handle UNC
paths (only relevant on Windows systems; Universal Naming Convention
paths are those that use the `\\host\path
' syntax).
New in version 1.5.2.
path, name) |
pathconf_names
dictionary. For configuration variables not
included in that mapping, passing an integer for name is also
accepted.
Availability: Macintosh, Unix.
If name is a string and is not known, ValueError is
raised. If a specific value for name is not supported by the
host system, even if it is included in pathconf_names
, an
OSError is raised with errno.EINVAL for the
error number.
path) |
os.path.join(os.path.dirname(path), result)
.
Availability: Macintosh, Unix.
path) |
path) |
src, dst) |
old, new) |
path) |
path) |
>>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo (33188, 422511L, 769L, 1, 1032, 100, 926L, 1105022698,1105022732, 1105022732) >>> statinfo.st_size 926L >>>
Changed in version 2.3: If stat_float_times returns true, the time values are floats, measuring seconds. Fractions of a second may be reported if the system supports that. On Mac OS, the times are always floats. See stat_float_times for further discussion. .
On some Unix systems (such as Linux), the following attributes may also be available: st_blocks (number of blocks allocated for file), st_blksize (filesystem blocksize), st_rdev (type of device if an inode device). st_flags (user defined flags for file).
On other Unix systems (such as FreeBSD), the following attributes may be available (but may be only filled out of root tries to use them: st_gen (file generation number), st_birthtime (time of file creation).
On Mac OS systems, the following attributes may also be available: st_rsize, st_creator, st_type.
On RISCOS systems, the following attributes are also available: st_ftype (file type), st_attrs (attributes), st_obtype (object type).
For backward compatibility, the return value of stat() is also accessible as a tuple of at least 10 integers giving the most important (and portable) members of the stat structure, in the order st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, st_ctime. More items may be added at the end by some implementations. The standard module stat defines functions and constants that are useful for extracting information from a stat structure. (On Windows, some items are filled with dummy values.)
Note: The exact meaning and resolution of the st_atime, st_mtime, and st_ctime members depends on the operating system and the file system. For example, on Windows systems using the FAT or FAT32 file systems, st_mtime has 2-second resolution, and st_atime has only 1-day resolution. See your operating system documentation for details.
Availability: Macintosh, Unix, Windows.
Changed in version 2.2: Added access to values as attributes of the returned object. Changed in version 2.5: Added st_gen, st_birthtime.
[newvalue]) |
For compatibility with older Python versions, accessing stat_result as a tuple always returns integers.
Changed in version 2.5: Python now returns float values by default. Applications which do not work correctly with floating point time stamps can use this function to restore the old behaviour.
The resolution of the timestamps (i.e. the smallest possible fraction) depends on the system. Some systems only support second resolution; on these systems, the fraction will always be zero.
It is recommended that this setting is only changed at program startup time in the __main__ module; libraries should never change this setting. If an application uses a library that works incorrectly if floating point time stamps are processed, this application should turn the feature off until the library has been corrected.
path) |
For backward compatibility, the return value is also accessible as a tuple whose values correspond to the attributes, in the order given above. The standard module statvfs defines constants that are useful for extracting information from a statvfs structure when accessing it as a sequence; this remains useful when writing code that needs to work with versions of Python that don't support accessing the fields as attributes.
Changed in version 2.2: Added access to values as attributes of the returned object.
src, dst) |
[dir[, prefix]]) |
None
. If given and not
None
, prefix is used to provide a short prefix to the
filename. Applications are responsible for properly creating and
managing files created using paths returned by tempnam();
no automatic cleanup is provided.
On Unix, the environment variable TMPDIR overrides
dir, while on Windows the TMP is used. The specific
behavior of this function depends on the C library implementation;
some aspects are underspecified in system documentation.
Warning:
Use of tempnam() is vulnerable to symlink attacks;
consider using tmpfile() (section 6.1.2)
instead. Availability: Macintosh, Unix, Windows.
) |
path) |
path, times) |
None
, then the file's access and modified
times are set to the current time. Otherwise, times must be a
2-tuple of numbers, of the form (atime, mtime)
which is used to set the access and modified times, respectively.
Whether a directory can be given for path depends on whether the
operating system implements directories as files (for example, Windows
does not). Note that the exact times you set here may not be returned
by a subsequent stat() call, depending on the resolution
with which your operating system records access and modification times;
see stat().
Changed in version 2.0:
Added support for None
for times.
Availability: Macintosh, Unix, Windows.
top[, topdown=True
[, onerror=None ]]) |
(dirpath, dirnames, filenames)
.
dirpath is a string, the path to the directory. dirnames is
a list of the names of the subdirectories in dirpath
(excluding '.'
and '..'
). filenames is a list of
the names of the non-directory files in dirpath. Note that the
names in the lists contain no path components. To get a full
path (which begins with top) to a file or directory in
dirpath, do os.path.join(dirpath, name)
.
If optional argument topdown is true or not specified, the triple for a directory is generated before the triples for any of its subdirectories (directories are generated top down). If topdown is false, the triple for a directory is generated after the triples for all of its subdirectories (directories are generated bottom up).
When topdown is true, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again. Modifying dirnames when topdown is false is ineffective, because in bottom-up mode the directories in dirnames are generated before dirnames itself is generated.
By default errors from the os.listdir()
call are ignored. If
optional argument onerror is specified, it should be a function;
it will be called with one argument, an os.error instance. It can
report the error to continue with the walk, or raise the exception
to abort the walk. Note that the filename is available as the
filename
attribute of the exception object.
os.path.islink(path)
, and invoke walk(path)
on each directly.
This example displays the number of bytes taken by non-directory files in each directory under the starting directory, except that it doesn't look under any CVS subdirectory:
import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print root, "consumes", print sum(getsize(join(root, name)) for name in files), print "bytes in", len(files), "non-directory files" if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories
In the next example, walking the tree bottom up is essential: rmdir() doesn't allow deleting a directory before the directory is empty:
# Delete everything reachable from the directory named in 'top', # assuming there are no symbolic links. # CAUTION: This is dangerous! For example, if top == '/', it # could delete all your disk files. import os for root, dirs, files in os.walk(top, topdown=False): for name in files: os.remove(os.path.join(root, name)) for name in dirs: os.rmdir(os.path.join(root, name))
New in version 2.3.
See About this document... for information on suggesting changes.