Python
Installation
Use Anaconda.
Basic Usage
How to use Python 3.
Ternary Operator
is_nice = True
state = "nice" if is_nice else "not nice"
Lambda Function
lambda x: x * 2
Filesystem Read and Write
List all files in a folder
gazeDir = "Gaze_txt_files"
# List of folders in root folder
gazeFolders = [path.join(gazeDir, x) for x in os.listdir(gazeDir)]
# List of files 2 folders down
gazeFiles = [path.join(x, y) for x in gazeFolders for y in os.listdir(x)]
Read entire text file into a list
with open('C:/path/numbers.txt') as f:
lines = f.read().splitlines()
Regular Expressions (Regex)
import re
myReg = re.compile(r'height:(\d+)cm')
myMatch = re.match(myReg, "height:33cm");
print(myMatch[1])
# 33
Spawning Processes
Use subprocess to spawn other programs.
import subprocess
subprocess.run(["ls", "-l"], cwd="/")