Tips for Learning Python Scripting [Example]

Tanner Jones
Nerd For Tech
Published in
3 min readMar 5, 2021

Tanner Jones @all-tech-guy

Image Source

Python is an object-oriented programming language, and it is among the most popular programming languages used today. There are a number of reasons why you should learn Python. I have previously written an article to help self-taught programmers. Check out that article here.

Python is a powerful programming language, and one of the best features of Python is readability. The syntax was designed to produce clean readable code that can be understood easily. The language was designed with several guiding principles in mind. These “guiding principles” are called the The Zen of Python:

  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • Simple is better than complex.
  • Complex is better than complicated.
  • Check here for the full list.

Though the entire list is what makes Python so powerful, what makes Python my favorite language is explained in the first 4 principles listed above. Whether you want to be a full stack developer or program as a hobby, using Python effectively can be extremely useful (and very fun)!

The Zen of Python ensures that the code produced is efficient and elegant, but overall easy to read. There are several ways to code an object using Python, but always remember that readability is key. That is why Python is great for scripting. Scripting creates short programs that are executed in order to automate routine tasks. Have you ever come across a task that you do everyday, every week, or every month that takes up a LOT of your time? I am sure your answer is YES, and that you would rather spend your time and effort doing something else! This is where Python scripting will come in and save the day! With these guiding principles in mind lets dig into a scripting example.

In this example, I will be creating a script to automate the creating of a folder directory. This folder directory was created to keep track of information gathered during a pen test. I have broken the script into 4 main parts.

By quickly skimming the code below, you can tell that it has main folders called “Documentation”, “Findings”, and “Data”. Within these folders, there are several folders created such as “Customer Info” and “Reports”. The folder structure can be changed very easily to accommodate new tools used or to add/remove unnecessary information. This can save a vast amount of time! Imagine having to manually create these folders each time a pen test is conducted or a similar folder structure had to be created for each customer of a business. This would take time, and errors could occur taking up more time, energy, and resources.

#PART 1: Import required modules

import os
def main():
#PART 2:Creating folder structure

"""Documentation"""
Doc = "1.Documentation"
Cust = "a. Customer Info"
Reports = "b. Reports"

#Findings Folder
"""Findings"""
Findings = "2.Findings"
Ext_F = "a. External"
Int_F = "b. Internal"
Phishing = "c. Phishing"

#Data Folder
"""Data"""
Data ="3.Data"
Database = "a. Database"
Phishing = "b. Phishing"
Templates = "i. Templates"
Payloads = "ii. Payloads"
Network = "c. Network Mapping"
ExtM = "i. External"
Nmap_E ="1. Nmap"
Eye_E = "2. Eyewitness"
Int_M = "Internal"
Nmap_I ="1. Nmap"
Eye_I = "2. Eyewitness"
Pen_Test = "d. Penetration Test"
Vul_Scan = "e. Vulnerability Scanning"
Ext_V = "i. External"
Nessus_E = "1. Nessus"
Internal_I = "1. Nessus"
Web_App = "f. Web App"
Burp = "i. Burp"
Nikto = "ii. Nikto"
#PART 3: Directory
#Where the directory will be exectuted
root = "<insert directory>"
#Part 4: Folder Creation
path = f"{root}/{Doc}/{Cust}/{Reports}"

"""
path = f"{root}/{Findings}/{Int_F}/{Ext_F}/{Phishing}"
path = f"{root}/{Data}/{Database}/{Phishing}/{Database}/{Phishing}"
"""
print(path)

if not os.path.exists(path):
os.makedirs(path)
else:
os.removedirs(path)

Towards the bottom of the code segment, you can change your root directory to where the folders will be created. The “if” statement will check and see if the path is already created or else it will create the path. Check out my github repository to get a copy of the code. This is just one example of how you can use Python to automate routine tasks by creating short and easy-to-read scripts. Now get out there and create a script yourself!

Be sure to comment your scripting ideas below.

Thanks for reading!

--

--

Tanner Jones
Nerd For Tech

I am passionate about technology and I am curious of how things work. I write to learn and help others learn about a variety of topics. I love the outdoors!