3

Scope: to produce a script that fetches the task list from windows cmd, stores it as an array, appends an identical timestamp to each task, and then appends it to a text file as a comma-delimited table so an analyst can load it into excel later.

Trouble: The question here is how to create an additional column in the array, and then populate it with the timestamp. The standard numpy functions (append) don't seem to quite do what I need, and accessing specific columns in an array and referencing them in a loop is still unclear to me. I'm new enough to arrays that I might be missing a key piece of logic

Code so far:

import os, sys, subprocess, numpy, csv, datetime


# Set the output location to a user provided location, where a txt simply named with the date will be stored for that day.
date_ = datetime.datetime.now()
date = date_.strftime('%Y%m%d')
timestamp = date_.strftime('%x %X')
output_ = raw_input("Please enter the directory where you would like the task list to be compiled:")
location = output_ +'\\tasklist_' + str(date) +'.txt'


# Create the array from tasklist
subprocess.check_output('tasklist /fo csv > %userprofile%\desktop\processes.csv')
arrays = [numpy.array(map(str, line.split(',')))for line in open('C:\Users\[USER]\Desktop\processes.csv')]


# Iterate over the array, and append the timestamp:
rows = len(x)
count = 0

for task in arrays:
    if count <= rows:
        count =+ 1
        ## ADD TIMESTAMP


# Check for output txt. If it exists, append to it. if it doesnt, write over it.
if os.path.isfile(location) == True:
    numpy.delete(arrays,(0),axis=0)
    with open(location, "a+") as save:
        save.write(arrays)
else:
    with open(location, "a+") as save:
        save.write(arrays)

Helpful mentions: This question here helped me set up the Task List procedure.

Thanks for viewing!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.