Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
36 lines (25 sloc) 1.62 KB

Session 3 Exercises

Reading/Writing Files in Python

Make sure you are familiar with how to read and write files in Python. Follow the tutorials below to get started.

Writing Command Line Scripts in Pyhton

  • For a very basic introduction, see this answer on Stack Overflow (be sure to read the question first)
    • Try to write and execute a basic Python script, using the command line.

First, create and save the code below as hello.py:

def hello():
    print("Hi :)")

if __name__ == "__main__":
    hello()

Then open a terminal, navigate to the location of the saved Python file (using cd command), and run:

python hello.py

Hopefully you see "Hi :)" printed directly to your console.

  • Passing command line arguments
    • You can pass variables to your Python script from the command line. For example, python hello.py "customVariable".
    • Read through the tutorial on the argparse module for more info: Using argparse.

Multiprocessing