in CODING, Python

Python: Build Application List

My first major Python script!

I created a script that took the list of applications from a standard build then compares to a CSV file from a users current machine, subtracts them and what’s left is the additional apps to add at build time.

It uses `csv.Sniffer` to work out what the delimiter is so it can detect if its ‘,’ or ‘;’ or tabs, etc. [Line 13 is where the heavy lifting is]

Here’s the script:


# Imports
import csv, os, pathlib, glob
from pprint import pprint

def read_csv_delimit(csv_file):
    # Initialise list
    file_csv = []
    # Open csv & check delimiter
    with open(csv_file, newline='', encoding = "ISO-8859-1") as csvfile:
        dialect = csv.Sniffer().sniff(
        reader = csv.reader(csvfile, dialect)
        for item in reader:
        #del file_csv[0]
        return file_csv

def write_csv(file_dir, csv_file, new_machine_list):
    # Open file then write the lines
    if os.path.exists(file_dir + "/" + csv_file):
        append_write = 'a'
        append_write = 'w'

    file_name = csv_file
    with open(file_dir + "/" + csv_file, "w") as csv_file:
        writer = csv.writer(csv_file, delimiter=',')
        for line in new_machine_list:

def split_path(full_path):
    #path = path.rstrip(os.sep)
    head, tail = os.path.split(full_path)
    return (head, tail)

def enumMachines(machine_dir):
    machines = []
    for filename in glob.glob(machine_dir + "/*.csv"):
    return machines

def main():
    # Get the paths to the csv files
    app_csv = input("drop the app list csv here: ")
    machine_dir = input("drop the machines csv folder here: ")

    # Gets list of machines with extension of .csv
    machines = enumMachines(machine_dir)

    # get csv data
    app_list = read_csv_delimit(app_csv)

    for machine in machines:

        # import machine csv data
        machine_list = read_csv_delimit(machine)
        # delete the first row
        del machine_list[0]

        # list comprehension
        new_machine_list = [app for app in machine_list if app not in app_list]

        # get the machine csv name
        new_machine_dir, new_machine_csv = split_path(machine)

        # new folder to write to
        new_machine_dir = machine_dir + "/" + "edited"

        # create the new folder if it doesnt exist
        pathlib.Path(new_machine_dir).mkdir(parents=True, exist_ok=True)

        # Write the machine name first
        #write_csv(new_machine_dir, new_machine_csv, new_machine_csv)

        # write to the new csv
        write_csv(new_machine_dir, new_machine_csv, new_machine_list)

if __name__ == '__main__': main()

Write a Comment