Category Archives: Python

How to Terminate a Python Script

The simplest way is to use quit(), i.e: // do something   quit()// do something quit() Another way is to use sys.exit(), i.e: import sys sys.exit()import sys sys.exit()

Read File Line by Line in Python

Example to read a file line by line in python: testsite_array = [] with open(‘file.txt’) as my_file: for line in my_file: testsite_array.append(line)testsite_array = [] with open(‘file.txt’) as my_file: for line in my_file: testsite_array.append(line) Alternatively, you may want to use f.readlines(): with open(‘file.txt’) as my_file: testsite_array = my_file.readlines()with open(‘file.txt’) as my_file: testsite_array = my_file.readlines()

Run Python Scripts Without Invoking `Python`

To nromally run python scriupts you need to invoke `python`, i.e: python script.pypython script.py However, if you include this text in the first line of your python script: #!/usr/bin/python#!/usr/bin/python Then you can run your python script with just: ./script.py./script.py Of course, you’ll need to first set +x permissions: chmod +x script.pychmod +x script.py If you […]

How to Install Python PIP on Debian

To manage Python packages you can use PIP. To install Python2 PIP just type: apt-get install python-pip #python 2apt-get install python-pip #python 2 To install Python3 PIP just type: apt-get install python-pip3 #python 3 or apt-get install python3-pip #python 3apt-get install python-pip3 #python 3 or apt-get install python3-pip #python 3 Here is an example on […]