Dino Geek, try to help you

How to use command line arguments in Python?


Command line arguments in Python are handled by a module named sys. The sys module provides functions and variables that can be used to manipulate parts of the Python runtime environment. The argv is one of the features provided by the sys module, and it’s a list in Python, which contains the command-line arguments passed to the script.

This is an example of how to use command line arguments:

```

  1. import the sys module
    import sys

print(“Name of the script: “, sys.argv0) # the script name (it could be the full path depending on how your program was started)

print(“Number of arguments: “, len(sys.argv)) # number of cmd arguments including the script name

print(“The arguments are: “ , str(sys.argv)) # full command-line
```

If you’ll run a script like this:

`python test.py arg1 arg2 arg3`

The output will be:

```
Name of the script: test.py
Number of arguments: 4
The arguments are: [‘test.py’, ‘arg1’, ‘arg2’, ‘arg3’]
```

sys.argv0 is always the name (or path) of the script itself. The actual command line arguments start from sys.argv1 to sys.argv[n].


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use