I'm trying to run a self-made program called add-entry.py which is taking 3 positional arguments. I understand that sys.argv[0] is the name of the program itself, when I intentionally call it without any other argument passed, I receive this error message filename = sys.argv[1] IndexError: list index out of range Which is true, but my code is taking care of that, see below: import sys import os # check the number of arguments args = sys.argv[1:] if not args or len(args) < 3: print("Usage: python add-entry.py <filename> <text...>") basedir = os.getcwd() filename = sys.argv[1] text_to_add = sys.argv[2:] fullpath = os.path.join(basedir, filename) On the opposite, when I provide the filename but not the text, it's handled by my code. What is happening there, how can I correct it ? Thanks Full code: import sys import os # check the number of arguments args = sys.argv[1:] if not args or len(args) < 3: print("Usage: python add-entry.py <filename> <text...>") basedir = os.getcwd() filename = sys.argv[1] text_to_add = sys.argv[2:] fullpath = os.path.join(basedir, filename) # split text arguments provided def split_text_args(): return [arg.strip() for arg in text_to_add] # split text from file def split_text_file(): with open(fullpath) as file: return [line.strip() for line in file] text_args = split_text_args() text_file = split_text_file() # check if text to add already exists in file, if not, append it and terminate the function for text in text_args: if text in text_file: print(f"'{text}' already exists.") else: with open(fullpath, 'a') as file: file.write(text + '\n') print(f"'{text}' successfully added to '{filename}'.") sorted(file) exit(0) Continue reading...