将日期时间追加到数组(Python)

时间:2018-07-20 12:12:30

标签: python arrays python-2.7 subprocess tasklist

作用域:生成一个脚本,该脚本从Windows cmd获取任务列表,将其存储为数组,为每个任务附加相同的时间戳,然后将其作为逗号附加至文本文件。分隔表,以便分析人员以后可以将其加载到excel中。

问题:这里的问题是如何在数组中创建其他列,然后使用时间戳填充它。标准的numpy函数(追加)似乎并不能完全满足我的需要,对我来说,访问数组中的特定列并在循环中引用它们仍然是未知的。我对阵列已经很陌生,可能会错过关键的逻辑

到目前为止的代码

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')]


# Iterrate through 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)

有用的提及:这个问题here帮助我设置了任务列表过程。

感谢您的观看!

0 个答案:

没有答案