Read io until the end of the stream/file and count the number of lines. To specify a file pass the filename as the first argument. EOL markers other than '\n' are supported by passing them as the second argument.


# Count the number of lines in a file
julia> countlines("myfile.txt")

# Count the number of lines in a stream
julia> io = IOBuffer("Hello\nWorld\nJulia\n");
julia> countlines(io)

# Count the number of lines with a custom end-of-line marker
julia> io = IOBuffer("Hello\rWorld\rJulia\r", '\r');
julia> countlines(io, '\r')

Common mistake example:

julia> countlines("nonexistent_file.txt")
ERROR: IOError: could not open file nonexistent_file.txt

In this example, the function throws an IOError because the specified file does not exist. Make sure to pass a valid file path or ensure that the file exists before calling countlines.

See Also

abspath, basename, chmod, countlines, cp, ctime, dirname, download, evalfile, expanduser, fdio, filemode, filesize, functionloc, gperm, homedir, include_string, isabspath, isblockdev, ischardev, isdir, isdirpath, isexecutable, isfifo, isfile, islink, ismount, ispath, isreadable, issetgid, issetuid, issticky, iswritable, joinpath, less, lstat, mkdir, mkpath, mktemp, mktempdir, mtime, mv, normpath, operm, poll_fd, poll_file, readall, readcsv, readdir, readdlm, readlines, readlink, realpath, relpath, rm, splitdir, splitdrive, splitext, stat, symlink, tempdir, tempname, touch, truncate, uperm, watch_file, writecsv,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field

Checking you are not a robot: