Using Python and Argparse to Build Quick Scripts

preview_player
Показать описание
Python is a really handy language for scripting, particularly when compared to a compiled language like C# or Java. Lets take a look at how we can use python and a python module called argparse to build simple python scripts that are extremely easy to use.

Argparse is a module for python that makes adding command line arguments to your programs extremely easy. It also helps with input validation and documentation by automatically generating error messages, parsing your input and automatically creating `--help` and `-h` commands to display help for your program.

You can install the argparse module using pip:

```bash
pip install argparse
```

A simple example of using argparse may look like this:

```python
import argparse

parser = argparse.ArgumentParser(description='print a list of integers')
help='one or more integers to print')

```

This will automatically validate that at least 1 integer is passed to the function and generate a help command that can be run using either `--help` or `-h`.

Рекомендации по теме
Комментарии
Автор

A quick refresher on the Python print function.

The following statement would have given you what you were looking for at the 17:00 minute mark in the video:
Python v2 -- print 'Writing to %s' % args.file
Python v3 -- print('Writing to %s' % args.file)

Technically, the item following the last percent sign (%) should be a tuple, so (args.file) and (args.file, ) would also work like this:
print 'Writing to %s % (args.file)
print 'Writing to %s % (args.file, )

Expanding this to include your 'text' variable:
Python v2 -- print 'Writing text: "%s" to file: %s' % (args.text, args.file)
Python v3 -- print('Writing text: "%s" to file: %s' % (args.text, args.file))
## The 's' "flag" following the percent sign (%) calls for a s(tring). There are other "flags" like f(loat) and i(nteger).

Regarding the string format() function which you ended up using, this is the "new" or more comprehensive tool for creating formatted output.
The only difference between Python v2 and Python v3 is the use of parenthesis around the arguments:
Python v2 -- print 'Writing to {}'.format(args.file)
Python v3 -- print('Writing to {}'.format(args.file))
or
Python v2 -- print 'Writing text: "{}" to file: {}'.format(args.text, args.file)
## An example, above, of when it's easier to use single quotes instead of double quotes for your string.
## The same output using all double quotes, but using the needed escape character (\) is below.
Python v3 -- print("Writing text: \"{}\" to file: {}".format(args.text, args.file))

The specification language for the format() function is found in the Python document library and includes a number of examples:

Finally, as Mihai Dinca mentioned, you forgot to close your file:
filehandle.close()

Thanks for your video!! It provides a great way to get started using the Python Standard Library argparse module.

Not bad for a Windows user. :-)

fracjackmac
Автор

Using a Jupyter Notebook in Python 3.6. Getting this error. From what I've read, it's related to a sysexit issue. What is the error?

usage: ipykernel_launcher.py [-h] -c CONFIG_FILE query
ipykernel_launcher.py: error: the following arguments are required: -c/--config-file




An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

cargouvu
Автор

You can pass the scene of Custom Particle Emitters in Unity 3D, it does not work out well. Nor using your scripts I get it right.

NN-njom
Автор

Great video. 1 qu.: Would it work if i execute the script as an .exe? Thx

fitnessneele-