Filename(3)
okay bestie, this is the Filename(3) page from the OCaml library manual, and it's literally all about operations on file names β¨π
πΈ NAME πΈ
Operations on file names. That's the whole assignment, no cap.
π§© Module π§©
The module is Filename β like, literally Module Filename. Main character module energy π
π Documentation π
Module Filename : sig end
Operations on file names. Yes, again β the docs said it twice and we are not skipping the βboringβ parts, bestie π«Ά
Here are all the vals, in order, with the signatures kept verbatim because that's the whole point:
val current_dir_name : stringβ the conventional name for the current directory (e.g..in Unix). It's giving βwhere am Iβ but make it a string π«Άval parent_dir_name : stringβ the conventional name for the parent of the current directory (e.g...in Unix). Iconic βgo up oneβ behavior.val dir_sep : stringβ the directory separator (e.g./in Unix).- Since
3.11.2.
- Since
val concat : string -> string -> stringβconcat dir filereturns a file name that designates filefilein directorydir. Yes, it's the βput these two togetherβ slay β¨val is_relative : string -> boolβ returnstrueif the file name is relative to the current directory, andfalseif it is absolute (i.e. in Unix, starts with/). Vibe check: relative or absolute?val is_implicit : string -> boolβ returnstrueif the file name is relative and does not start with an explicit reference to the current directory (./or../in Unix);falseif it starts with an explicit reference to the root directory or the current directory. Lowkey βis this path being sneaky about where it starts?βval check_suffix : string -> string -> boolβcheck_suffix name suffreturnstrueif the filenamenameends with the suffixsuff.- Under Windows ports (including Cygwin), comparison is case-insensitive, relying on
String.lowercase_ascii. Note that this does not match exactly the interpretation of case-insensitive filename equivalence from Windows.
- Under Windows ports (including Cygwin), comparison is case-insensitive, relying on
val chop_suffix : string -> string -> stringβchop_suffix name suffremoves the suffixsufffrom the filenamename.- Raises
Invalid_argumentifnamedoes not end with the suffixsuff.
- Raises
val chop_suffix_opt : suffix:string -> string -> string optionβchop_suffix_opt ~suffix filenameremoves the suffix from the filename if possible, or returnsNoneif the filename does not end with the suffix.- Under Windows ports (including Cygwin), comparison is case-insensitive, relying on
String.lowercase_ascii. Note that this does not match exactly the interpretation of case-insensitive filename equivalence from Windows. - Since
4.08.
- Under Windows ports (including Cygwin), comparison is case-insensitive, relying on
val extension : string -> stringβextension nameis the shortest suffixextofname0where:name0is the longest suffix ofnamethat does not contain a directory separator;extstarts with a period;extis preceded by at least one non-period character inname0. If such a suffix does not exist,extension nameis the empty string.- Since
4.04.
val remove_extension : string -> stringβ returns the given file name without its extension, as defined inFilename.extension. If the extension is empty, the function returns the given file name.- The following invariant holds for any file name
s:remove_extension s ^ extension s = s - Since
4.04.
- The following invariant holds for any file name
val chop_extension : string -> stringβ same asFilename.remove_extension, but raiseInvalid_argumentif the given name has an empty extension.val basename : string -> stringβ split a file name into directory name / base file name. Ifnameis a valid file name, thenconcat (dirname name) (basename name)returns a file name which is equivalent toname. Moreover, after setting the current directory todirname name(withSys.chdir), references tobasename name(which is a relative file name) designate the same file asnamebefore the call toSys.chdir.- This function conforms to the specification of POSIX.1-2008 for the
basenameutility.
- This function conforms to the specification of POSIX.1-2008 for the
val dirname : string -> stringβ seeFilename.basename. This function conforms to the specification of POSIX.1-2008 for thedirnameutility.val null : stringβnullis"/dev/null"on POSIX and"NUL"on Windows. It represents a file on the OS that discards all writes and returns end of file on reads.- Since
4.10.
- Since
val temp_file : ?temp_dir:string -> string -> string -> stringβtemp_file prefix suffixreturns the name of a fresh temporary file in the temporary directory. The base name of the temporary file is formed by concatenatingprefix, then a suitably chosen integer number, thensuffix. The optional argumenttemp_dirindicates the temporary directory to use, defaulting to the current result ofFilename.get_temp_dir_name. The temporary file is created empty, with permissions0o600(readable and writable only by the file owner). The file is guaranteed to be different from any other file that existed whentemp_filewas called.- Before
3.11.2, there was no?temp_diroptional argument. - Raises
Sys_errorif the file could not be created.
- Before
val open_temp_file : ?mode:open_flag list -> ?perms:int -> ?temp_dir:string -> string -> string -> string * out_channelβ same asFilename.temp_file, but returns both the name of a fresh temporary file and an output channel opened (atomically) on this file. This function is more secure thantemp_file: there is no risk that the temporary file will be modified (e.g. replaced by a symbolic link) before the program opens it. The optional argumentmodeis a list of additional flags to control the opening of the file. It can contain one or several ofOpen_append,Open_binary, andOpen_text. The default is[Open_text](open in text mode). The file is created with permissionsperms(defaults to readable and writable only by the file owner,0o600).- Before
4.03, there was no?permsoptional argument. - Before
3.11.2, there was no?temp_diroptional argument. - Raises
Sys_errorif the file could not be opened.
- Before
val temp_dir : ?temp_dir:string -> ?perms:int -> string -> string -> stringβtemp_dir prefix suffixcreates and returns the name of a fresh temporary directory with permissionsperms(defaults to0o700) insidetemp_dir. The base name of the temporary directory is formed by concatenatingprefix, then a suitably chosen integer number, thensuffix. The optional argumenttemp_dirindicates the temporary directory to use, defaulting to the current result ofFilename.get_temp_dir_name. The temporary directory is created empty, with permissions0o700(readable, writable, and searchable only by the file owner). The directory is guaranteed to be different from any other directory that existed whentemp_dirwas called.- If
temp_dirdoes not exist, this function does not create it. Instead, it raisesSys_error. - Since
5.1. - Raises
Sys_errorif the directory could not be created.
- If
val get_temp_dir_name : unit -> stringβ the name of the temporary directory. Under Unix, the value of theTMPDIRenvironment variable, or"/tmp"if the variable is not set.- Under Windows, the value returned by
GetTempPath2(if available) orGetTempPath. - The temporary directory can be changed with
Filename.set_temp_dir_name. - Under Windows, before OCaml
5.4, it would return the value of theTEMPenvironment variable, or"."if the variable was not set. - Since
4.00.
- Under Windows, the value returned by
val set_temp_dir_name : string -> unitβ change the temporary directory returned byFilename.get_temp_dir_nameand used byFilename.temp_fileandFilename.open_temp_file. The temporary directory is a domain-local value which is inherited by child domains.- Since
4.00.
- Since
val quote : string -> stringβ return a quoted version of a file name, suitable for use as one argument in a command line, escaping all meta-characters.- Warning: under Windows, the output is only suitable for use with programs that follow the standard Windows quoting conventions.
val quote_command : string -> ?stdin:string -> ?stdout:string -> ?stderr:string -> string list -> stringβquote_command cmd argsreturns a quoted command line, suitable for use as an argument toSys.command,Unix.system, and theUnix.open_processfunctions.- The string
cmdis the command to call. The listargsis the list of arguments to pass to this command. It can be empty. - The optional arguments
?stdin,?stdout, and?stderrare file names used to redirect the standard input, the standard output, or the standard error of the command. - If
~stdin:fis given, a redirection< fis performed and the standard input of the command reads from filef. - If
~stdout:fis given, a redirection> fis performed and the standard output of the command is written to filef. - If
~stderr:fis given, a redirection2> fis performed and the standard error of the command is written to filef. - If both
~stdout:fand~stderr:fare given, with the exact same file namef, a2>&1redirection is performed so that the standard output and the standard error of the command are interleaved and redirected to the same filef. - Under Unix and Cygwin, the command, the arguments, and the redirections if any are quoted using
Filename.quote, then concatenated. Under Win32, additional quoting is performed as required by thecmd.exeshell that is called bySys.command. - Since
4.10. - Raises
Failureif the command cannot be escaped on the current platform.
- The string
π sign-off time: source/version is OCamldoc, date is 2026-06-22, and the manual is Filename(3). Bye bestie, go name some files! π«Άβ¨
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms