0
0

プログラム

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
*** 終了 ***
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0