[Python] Read command line arguments from file name or stdin

Read arguments from filename or stdin


#!/usr/bin/env python3
"""
If the argument is a file, the contents of the file,
Otherwise print the arguments
"""
import sys
import fileinput
from pathlib import Path

if Path(sys.argv[1]).exists():  #If the first argument is a file
    for line in fileinput.input():  #Print the contents of the file line by line
        print('file input')
        print(line)
else:  #If the first argument is not a file
    for i in sys.argv[1:]:  #Print the argument string
        print('args input')
        print(i)

Execution result


$ python stdin_test.py hoge.txt fuga.txt
file input
MYNAME!

file input
HERO!!


$ python stdin_test.py 1 2 3
args input
1
args input
2
args input
3

Since sys.argv is returned as a list, I used it so that the result offileinput.input ()is also returned in list format.

Read arguments from filename or stdin and return as a list


if Path(sys.argv[1]).exists():  #If the first argument is a file
    ARGV = [
        line.replace('\n', '') for line in fileinput.input()  #Delete line breaks
        if line != '\n'  #Delete blank lines
    ]
else:  #If the first argument is not a file
    ARGV = sys.argv[1:]

Python official doc

Recommended Posts

[Python] Read command line arguments from file name or stdin
[Python] Read From Stdin
Read line by line from a file with Python
Read the file line by line in Python
Read the file line by line in Python
Decompose command arguments in one line in Python
[Python] Read the specified line in the file
Python / numpy> Read the data file with the item name line> Use genfromtxt ()
[Implementation example] Read the file line by line with Cython (Python) from the last line
[Introduction to Udemy Python3 + Application] 67. Command line arguments
Execute command from Python
Execute command from python
Read Python csv file
Read QR code from image file with Python (Mac)
How to pass arguments when invoking python script from blender on the command line
How to read text by standard input or file name specification like cat in Python
Twitter post from command line
python Note: Determine if command line arguments are in the list
How to get a string from a command line argument in python
Read a file in Python with a relative path from the program
Receive by standard input or send Gmail from command line operation
Execute Python script from batch file
Command line argument processing (Python docopt)
Read and use Python files from Python
Have python read the command output
Read fbx from python with cinema4d
Read the file with python and delete the line breaks [Notes on reading the file]
[Note] Read a file from another directory
Read CSV file with python (Download & parse CSV file)
Let's read the RINEX file with Python â‘ 
[Python / Tkinter] How to pass arguments to command
Create a deb file from a python package
Homebrew search from python module name (ShellScript)
Call a command from Python (Windows version)
[python] Read html file and practice scraping
[Automation] Read mail (msg file) with Python
[Python] (Line) Extract values from graph images
Various ways to read the last line of a csv file in Python
It's easier to iterate over a python file from a command prompt (cmd) .bat
How to manage arguments when implementing a Python script as a command line tool
I want to get the file name, line number, and function name in Python 3.4