我设法找到了使用外部脚本将文件从一个文件扩展名转换为另一个文件扩展名(从.evtx到.xml)的方法。以下是我正在使用的:
os.system("file_converter.py file1.evtx > file1.xml")
这使用我称为(file_converter.py)的外部脚本成功地将文件从.txt转换为.xml。
我现在正在尝试找到一种方法来说明如何使用“ os.system”,或者可能使用另一种方法一次转换多个文件,我希望程序进入一个文件夹并转换所有10个文件我一下子以.xml格式。
我的问题是,这是怎么可能的,因为os.system仅接受1个参数,而且我不确定如何通过目录定位它,这与我转换的第一个文件不在标准主目录中不同,但是该文件夹我想使用另一个文件夹中的10个文件进行访问,我试图找到一种解决该参数的方法,并且要一次完成转换,我还希望每个文件的文件名都保持不变唯一的不同是最后将“ .xml”从“ .evtx”更改为“ .xml”。
可从此处下载文件“ file_converter.py”
import threading
import os
def file_converter(file):
os.system("file_converter.py {0} > {1}".format(file, file.replace(".evtx", ".xml")))
base_dir = "C:\\Users\\carlo.zanocco\\Desktop\\test_dir\\"
for file in os.listdir(base_dir):
threading.Thread(target=file_converter, args=(file,)).start()
这是我的示例代码。您可以生成多个线程来“同时”运行操作。该程序将检查目录中的所有文件并进行转换。
现在我们有了有关您想要的更多信息,我可以为您提供帮助。该程序可以同时处理一个文件夹中的多个文件,也可以将其检入子文件夹。
import subprocess
import os
base_dir = "C:\\Users\\carlo.zanocco\\Desktop\\test_dir\\"
commands_to_run = list()
#Search all files
def file_list(directory):
allFiles = list()
for entry in os.listdir(directory):
fullPath = os.path.join(directory, entry)
#if is directory search for more files
if os.path.isdir(fullPath):
allFiles = allFiles + file_list(fullPath)
else:
#check that the file have the right extension and append the command to execute later
if(entry.endswith(".evtx")):
commands_to_run.append("C:\\Python27\\python.exe file_converter.py {0} > {1}".format(fullPath, fullPath.replace(".evtx", ".xml")))
return allFiles
print "Searching for files"
file_list(base_dir)
print "Running conversion"
processes = [subprocess.Popen(command, shell=True) for command in commands_to_run]
print "Waiting for converted files"
for process in processes:
process.wait()
print "Conversion done"
子流程模块可以以两种方式使用:
如果要解决所有问题,只需在程序中实现从github共享的代码即可。您可以轻松地将其实现为功能。
import threading
import os
import Evtx.Evtx as evtx
import Evtx.Views as e_views
base_dir = "C:\\Users\\carlo.zanocco\\Desktop\\test_dir\\"
def convert(file_in, file_out):
tmp_list = list()
with evtx.Evtx(file_in) as log:
tmp_list.append(e_views.XML_HEADER)
tmp_list.append("<Events>")
for record in log.records():
try:
tmp_list.append(record.xml())
except Exception as e:
print(e)
tmp_list.append("</Events>")
with open(file_out, 'w') as final:
final.writelines(tmp_list)
#Search all files
def file_list(directory):
allFiles = list()
for entry in os.listdir(directory):
fullPath = os.path.join(directory, entry)
#if is directory search for more files
if os.path.isdir(fullPath):
allFiles = allFiles + file_list(fullPath)
else:
#check that the file have the right extension and append the command to execute later
if(entry.endswith(".evtx")):
threading.Thread(target=convert, args=(fullPath, fullPath.replace(".evtx", ".xml"))).start()
return allFiles
print("Searching and converting files")
file_list(base_dir)
如果要显示文件生成,则只需按上述进行编辑:
def convert(file_in, file_out):
tmp_list = list()
with evtx.Evtx(file_in) as log:
with open(file_out, 'a') as final:
final.write(e_views.XML_HEADER)
final.write("<Events>")
for record in log.records():
try:
final.write(record.xml())
except Exception as e:
print(e)
final.write("</Events>")
如果要在转换后删除“ .evtx”文件,只需在convert
函数末尾添加以下行:
try:
os.remove(file_in)
except(Exception, ex):
raise ex
这里只需要使用,try .. except
因为仅当输入值是文件时才运行线程。如果文件不存在,此函数将引发异常,因此有必要首先检查os.path.isfile()
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句