0

Python Version: 3.11.4

I'm using a VENV called FontTools with all my dependencies installed.

Script:

import os.path
import sys
from fontTools import ttLib
from fontmeta import FontMeta

if len(sys.argv) > 1:
    print("Usage: Only one argument required: [FONT]")
    exit(1)

fontfile = sys.argv[1]
meta_instance = FontMeta(fontfile)
metadata = meta_instance.get_full_data()

print(metadata)

Usage: D:\Dev\Python\Font Scripts> python getFontMeta.py "SharpSans-Bold.otf"

Returns: Usage: Only one argument required: [FONT]

I inspected sys.argv[0], and it's set to the name of my script: getFontMeta.py sys.argv[1] is set to the font I passed in.

Why is this happening? This script worked before, is this new behavior in Python v3.11.4?

Thanks for any help.

fmotion1
  • 237
  • 4
  • 12
  • 4
    The filename is the first argument. It's always been this way. So you expect exactly 2 arguments. – juanpethes Aug 03 '23 at 19:52
  • Ok, weird. I could have sworn this script was working before as-is. I didn't know Python behaved that way. Thanks for the answer. – fmotion1 Aug 03 '23 at 19:53
  • No problem, your script probably worked if you didn't have the length check, because you correctly access the fontfile at index 1, with index 0 being the filename. Here is a SO question that asks the same for Python 3.2 indicating its been there for long: https://stackoverflow.com/questions/5222408/python-sys-argv0-meaning-in-official-documentation – juanpethes Aug 03 '23 at 19:56
  • Just for the record, this behavior was taken from the C language, going back more than 50 years. C programs take two parameters `(argc, argv)`, where `argc` is the count of elements, `argv[0]` is always the program name. This is not new behavior. – Tim Roberts Aug 03 '23 at 20:04
  • @TimRoberts Thanks for the history lesson! Good information to know. – fmotion1 Aug 03 '23 at 20:13

0 Answers0