[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

25.6.1 Testing Accessibility

These functions test for permission to access a file in specific ways.

Function: file-exists-p filename
This function returns t if a file named filename appears to exist. This does not mean you can necessarily read the file, only that you can find out its attributes. (On Unix and GNU/Linux, this is true if the file exists and you have execute permission on the containing directories, regardless of the protection of the file itself.)

If the file does not exist, or if fascist access control policies prevent you from finding the attributes of the file, this function returns nil.

Function: file-readable-p filename
This function returns t if a file named filename exists and you can read it. It returns nil otherwise.

 
(file-readable-p "files.texi")
     => t
(file-exists-p "/usr/spool/mqueue")
     => t
(file-readable-p "/usr/spool/mqueue")
     => nil

Function: file-executable-p filename
This function returns t if a file named filename exists and you can execute it. It returns nil otherwise. On Unix and GNU/Linux, if the file is a directory, execute permission means you can check the existence and attributes of files inside the directory, and open those files if their modes permit.

Function: file-writable-p filename
This function returns t if the file filename can be written or created by you, and nil otherwise. A file is writable if the file exists and you can write it. It is creatable if it does not exist, but the specified directory does exist and you can write in that directory.

In the third example below, `foo' is not writable because the parent directory does not exist, even though the user could create such a directory.

 
(file-writable-p "~/foo")
     => t
(file-writable-p "/foo")
     => nil
(file-writable-p "~/no-such-dir/foo")
     => nil

Function: file-accessible-directory-p dirname
This function returns t if you have permission to open existing files in the directory whose name as a file is dirname; otherwise (or if there is no such directory), it returns nil. The value of dirname may be either a directory name or the file name of a file which is a directory.

Example: after the following,

 
(file-accessible-directory-p "/foo")
     => nil

we can deduce that any attempt to read a file in `/foo/' will give an error.

Function: access-file filename string
This function opens file filename for reading, then closes it and returns nil. However, if the open fails, it signals an error using string as the error message text.

Function: file-ownership-preserved-p filename
This function returns t if deleting the file filename and then creating it anew would keep the file's owner unchanged.

Function: file-newer-than-file-p filename1 filename2
This function returns t if the file filename1 is newer than file filename2. If filename1 does not exist, it returns nil. If filename2 does not exist, it returns t.

In the following example, assume that the file `aug-19' was written on the 19th, `aug-20' was written on the 20th, and the file `no-file' doesn't exist at all.

 
(file-newer-than-file-p "aug-19" "aug-20")
     => nil
(file-newer-than-file-p "aug-20" "aug-19")
     => t
(file-newer-than-file-p "aug-19" "no-file")
     => t
(file-newer-than-file-p "no-file" "aug-19")
     => nil

You can use file-attributes to get a file's last modification time as a list of two numbers. See section 25.6.4 Other Information about Files.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on May 2, 2002 using texi2html