Python

Writing to CSV

To write to a CSV file

import csv

header = ['name', 'area', 'country_code2', 'country_code3']
data = ['Afghanistan', 652090, 'AF', 'AFG']


with open("C:\\Users\\Nicholas Goh\\Desktop\\test.csv", 'w', encoding='UTF8', newline='') as f:
    writer = csv.writer(f)

    # write the header
    writer.writerow(header)

    # write the data
    writer.writerow(data)

Change 'w' to 'a' for Append instead of Overwrite

Change "C:\\Users\\Nicholas Goh\\Desktop\\test.csv" to your CSV output location

Creating and calling a Function

Single Argument

def AFunctionSample(SampleArg):
 print(SampleArg)

AFunctionSample("Hello World!")

Multiple Arguments

def AFunctionSample(SampleArg1, SampleArg2):
 print(SampleArg1 + SampleArg2)

AFunctionSample("Hello World!", " It's Me!")

 

Note: The Function's Definition should always be before the Function's Call

Taking a screenshot

Full Screen

import pyautogui

pyautogui.screenshot("C:\\Users\\Nicholas Goh\\Desktop\\test.png")

Specific areas to screenshot can be specified by using the Region argument

import pyautogui

pyautogui.screenshot("C:\\Users\\Nicholas Goh\\Desktop\\test.png", region=(0, 0, 100, 200))

 

Replace C:\\Users\\Nicholas Goh\\Desktop\\test.png  with your own file path and file name. Double backslash in the file path \\ is required as \ is an escape character

 

Region requires an integer value and signifies in order: Left, Top, Width from Left, Height from Top

Generating a Timestamp

To generate a timestamp

from datetime import datetime

TimeStamp = datetime.now().strftime("%Y%m%d-%H%M%S")
print(TimeStamp)

%Y Year in yyyy

%y Year in yy

%m Month in mm

%d Day in dd

%H Hour in HH

%M Minute in MM

%S Seconds in SS