Create empty file

Empty files can be used as a mean to pass information between programs in a persistent manner, or a form of configuration. Empty files can be used on web servers to declutter 404 logs of endless bot scans.

For the examples below, we assume relative filename “path/to/empty”, where directories “path/to” already exist. We assume that a file does NOT exist at the filename, and it may be overwritten if it exists.

NOTE: For simplicity, some of these examples overwrite existing files, but some do not overwrite. Extra code is required to give a consistent behavior. All examples at least create an empty file if one doesn’t exist.

Windows users often use Command Prompt or PowerShell. Create empty file with Command Prompt:

copy NUL path\to\empty

Create empty file with PowerShell (on any operating system):

New-Item path/to/empty

For Linux, MacOS and other Unix-like systems, most shells create empty file like:

touch path/to/empty

Python creates an empty file by:

pathlib.Path('path/to/empty').touch()

Fortran creates an empty file by:

open(newunit=u, file='path/to/empty')
close(u)

Create an empty file in C++:

std::ofstream output("path/to/empty");