プログラム
line_count.py
#! /usr/bin/python
#
# text/line_count.py
#
# Jun/24/2024
#
# --------------------------------------------------------------------
import sys
import subprocess
#
# --------------------------------------------------------------------
def count_lines(filename):
try:
result = subprocess.run(['wc', '-l', filename], capture_output=True, text=True)
output = result.stdout.strip()
line_count = int(output.split()[0])
return line_count
except FileNotFoundError:
print(f"Error: ファイル {filename} が見つかりません。")
return None
except Exception as e:
print(f"Error: {e}")
return None
# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
file_in = sys.argv[1]
nn = count_lines(file_in)
print(nn)
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------
実行方法
$ ./line_count.py in01.txt
*** 開始 ***
501
*** 終了 ***