Read file

2 snippets across 2 stacks — Bash & Linux, Python

SHBash & Linux

Display File Contents

SH · File Content
syntax
cat [options] file...
example
cat server.log
cat -n deploy.sh
cat header.html body.html footer.html > page.html

Note -n adds line numbers, -A shows invisible characters (tabs, line endings). cat is best for short files; use less for anything longer than a screenful. Concatenating multiple files into one is where cat gets its name.

PYPython

Reading Files

PY · File I/O
syntax
with open(path, 'r') as f:
    content = f.read()
example
with open("config.txt", "r", encoding="utf-8") as f:
    content = f.read()
    print(content[:50])

# Read line by line (memory efficient)
with open("data.log") as f:
    for line in f:
        print(line.strip())

Note Always specify encoding='utf-8' explicitly. The default varies by platform. Iterating line-by-line avoids loading the entire file into memory.