Bootstrap FreeKB Articles

Python (Scripting) articles


Return full path to the Python interpreter using sys.executable base64 Base64 encoding and decoding Booleans Getting Started with Booleans Bytes Convert byte to string Classes Calling classes from another Python script Getting Starting with Classes Command line options Command line options flags arguments using sys.argv Commands Configure the "python" command to invoke the "python3" command Install module using setup.py Date and Time Date and Time Determine Time Difference def (function) Calling functions from another Python script Getting Started with functions Pass values into a function Return function name Return value in a function Dictionary Append to a dictionary Getting Started with Dictionaries Remove keys from a dictionary Email Send an email Sending email using SendGrid exit Stop Halt or Exit Files and Directories Copy file using shutil Create Write or Append to a file (open) Determine file encoding (ascii utf) Determine file MIME type Determine file or directory owner Determine if a file or directory exists Determine if an file or directory is readable writable (os.access) Determine if an object is a file or directory (os.path.isfile isdir) dirname basename and present working directory (pwd) Find files List files and directories mkdir Read a file (open) Read and Write to the same file Remove or Delete file or directory Write JSON dictionary to a file FTP Connect to an FTP Server Hashes Getting Started with Hashes hash has_key hostname hostname (socket) if elif else statement if elif else statements Install Install pycharm on Linux Install Python in a virtual environment on Linux Install Python on Linux JSON Determine if JSON key contains a value Determine if JSON key contains an empty array Determine if JSON key exists Getting Started with JSON Loop through JSON array Parse dictionary using json.dumps Parse dictionary using json.dumps and json.loads Parse JSON file using json.load Parse JSON using json.loads Parse JSON using json.tool Pass argparse into JSON Write a dictionary to a JSON file Lists Append values to the end of a list Blank out a list Combine or merge lists Convert list to string Convert sting to list Count the elements in a list Getting Started with Lists Remove duplicate values from a list Remove elements from a list Return the last item in a list Log Logging to a log file Loops Break out of a loop Loop over a dictionary Looping over a list Looping over a sequence of integers using range Looping over a variable using split or splitlines Move onto the next item in a list using continue while loop match Find matching expression using find Find matching expression using re.match Math Addition and Increment (+) Division (/) Subtraction and Decrement Modules Creating your own modules import module List Python system paths using sys.path Native operating system commands Native operating system commands new lines Remove new lines using strip, lstrip and rstrip os.path Return and print script name Printing output Printing text to the console (print) Random Generate random numbers and strings Replace Escape backslash Replace and re.sub REST API (curl) GET request PATCH request POST request SCP SCP using paramiko Sleep Pause using Sleep SSH SSH using paramiko strip Remove whitespace using strip, lstrip and rstrip tbd Command line options flags arguments using argparse Tokens Generate random secret tokens Troubleshooting Resolve "AttributeError: 'dict' object has no attribute 'loads'" Resolve "Python.h: No such file or directory" Resolve "TypeError: cannot concatenate 'str' and 'bool' objects" Resolve "TypeError: cannot concatenate 'str' and 'list' objects" Resolve "TypeError: string indices must be integers" Resolve KeyError Resolve ModuleNotFoundError Resolve UnicodeDecodeError try except else try except else finally Upper Lower Case Upper Lower Case using upper lower Users and Groups Return username using getpass getuser (whoami) Return users group using os.getgroups Users Home Directory Variables Append to a variable Defining variables Version python --version command Virtual Environment Activate a virtual environment Create a virtual environment (venv virtualenv) Installing packages in a virtual environment Setting environment variables in a virtual environment (venv virtualenv) winrm Connect to Windows Host YAML Modify YAML file ruamel.yaml Read YAML file using ruamel.yaml Write dictionary to YAML file