Skip to content

Instantly share code, notes, and snippets.

@bsnux
Last active March 3, 2023 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bsnux/3805696ae4a637fae52981cd6e295b5d to your computer and use it in GitHub Desktop.
Save bsnux/3805696ae4a637fae52981cd6e295b5d to your computer and use it in GitHub Desktop.
Useful Python one-liners
# Reading file lines from stdin
cat /tmp/test.txt | python -c 'import fileinput; [print(line, end="" for line in fileinput.input()]
python -c 'import fileinput; [print(line, end="") for line in fileinput.input()]' < /tmp/test.txt
# Printing the 3 lines of the file
python -c "import sys; sys.stdout.write(''.join(sys.stdin.readlines()[:3]))" < /tmp/test.txt
# Checking if file exists
python -c 'from pathlib import Path; print("yes") if Path("/tmp/metrics.txt").is_file() else print("no")'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment