Chapter 15
The os Module
On this page
Picat has an os module for manipulating files and directories. In order to use any of the functions or predicates, users must import the module.
15.1 The Parameter
Many of the functions and predicates in this module have a parameter. This parameter is a string or an atom, representing the path of a file or directory. This path can be an absolute path, from the system’s root directory, or a path relative to the current working directory. Different systems use different separator characters to separate directories at different levels of the directory hierarchy. For example, Windows uses ‘’ and Unix uses ‘/’. The following function outputs a single character, representing the character that the current system uses as a file separator.
15.2 Directories
The os module includes functions for reading and modifying directories. The following example shows how to list all of the files in a directory tree, using a depth-first directory traversal.
Example
import os.
traverse(Dir), directory(Dir) =>
List = listdir(Dir),
printf("Inside %s%n", Dir),
foreach (File in List)
printf(" %s%n", File)
end,
foreach (File in List, File != ".", File != "..")
FullName = Dir ++ [separator()] ++ File,
traverse(FullName)
end.
traverse(_Dir) => true.
The following function can be used to read the contents of a directory:
- listdir() = : This function returns a list of all of the files and directories that are contained inside the directory specified by . If is not a directory, then an error is thrown. The returned list contains strings, each of which is the name of a file or directory.
The above example also uses the directory predicate, which will be discussed in Section 15.4.
15.2.1 The Current Working Directory
The os module includes built-ins for obtaining the program’s current working directory, inspecting files in the directory, and changing the directory.
- cd(): This predicate changes the current working directory to the directory specified by . It fails if the directory does not exist, if the program lacks permission to it, or if does not refer to a directory.
- chdir(): This predicate is the same as cd().
- cwd() = : This function returns the current working directory.
- dir: This predicate lists the files in the current working directory.
- ls: This predicate is the same as dir.
- pwd() = : This function is the same as cwd().
15.3 Modifying Files and Directories
15.3.1 Creation
The os module contains a number of predicates for creating new files and directories:
- mkdir(): This predicate creates a new directory at location . The directory will be created with a default permission list of [rwu, rwg, ro]. If the program does not have permission to write to the parent directory of , this predicate will throw an error. An error will also occur if the parent directory does not exist.
- rename(, ): This renames a file or a directory from to . This predicate will throw an error if does not exist. An error will also occur if the program does not have permission to write to or .
- cp(, ): This copies a file from to . This predicate will throw an error if does not exist or is a directory. An error will also occur if the program does not have permission to read from , or if it does not have permission to write to .
15.3.2 Deletion
The os module contains a number of predicates for deleting files and directories.
- rm(): This deletes a file. An error will be thrown if the file does not exist, if the program does not have permission to delete the file, or if refers to a directory, a hard link, a symbolic link, or a special file type.
- rmdir(): This deletes a directory. An error will be thrown if the directory does not exist, if the program lacks permission to delete it, if it is not empty, or if does not refer to a directory.
15.4 Obtaining Information about Files
The os module contains a number of functions that retrieve file status information, and predicates that test the type of a file. Unless specified otherwise, these predicates will all throw an error if the program does not have permission to read from .
- readable(): Is the program allowed to read from the file?
- writable(): Is the program allowed to write to the file?
- executable(): Is the program allowed to execute the file?
- size() = : If is not a symbolic link, then this function returns the number of bytes contained in the file to which refers. If is a symbolic link, then this function returns the path size of the symbolic link. Because the function size/1 is defined in the basic module for returning the size of a map, this function requires an explicit module qualifier os.size().
- file_base_name() = : This function returns a string containing the base name of . For example, the base name of "a/b/c.txt" is "c.txt".
- file_directory_name() = : This function returns a string containing the path of the directory that contains . For example, the directory name of "a/b/c.txt" is "a/b".
- exists(): Is an existing file or directory?
- file(): Does refer to a regular file? This predicate is true if is neither a directory nor a special file, such as a socket or a pipe.
- file_exists(): This tests whether exists, and, if it exists, whether refers to a regular file.
- directory(): Does refer to a directory?
The following example shows how to use a few of the predicates.
Example
import os.
test_file(Path) =>
if (not exists(Path))
printf("%s does not exist %n", Path)
elseif (directory(Path))
println("Directory")
elseif (file(Path))
println("File")
else
println("Unknown")
end.