如何使用os.system使用外部python脚本一次转换文件夹中的所有文件

N2这

我设法找到了使用外部脚本将文件从一个文件扩展名转换为另一个文件扩展名(从.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”

卡罗·扎诺科(Carlo Zanocco)
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()

这是我的示例代码。您可以生成多个线程来“同时”运行操作。该程序将检查目录中的所有文件并进行转换。

编辑python2.7版本

现在我们有了有关您想要的更多信息,我可以为您提供帮助。该程序可以同时处理一个文件夹中的多个文件,也可以将其检入子文件夹。

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"

子流程模块可以以两种方式使用:

  1. subprocess.Popen:它运行流程并继续执行
  2. subprocess.call:它运行进程并等待它,此函数返回退出状态。如果该值为零,则表明进程成功终止

编辑python3.7版本

如果要解决所有问题,只需在程序中实现从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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Groovy读取文件夹中的所有文件并替换文件中的模式

如何使用python和os合并特定文件夹中的所有csv文件

使用 opus 转换文件夹

如何使用python删除文件夹中的所有文件夹?

如何使用 os.scandir 循环同一文件夹中的所有光栅文件

如何通过使用终端或脚本隐藏文件夹中的所有文件

如何使用Java脚本打印文件夹中的所有txt文件

我们如何一次对所有子文件夹执行终端命令

我有一个脚本,可以对 .xlsx 文件进行一些操作。如何使用文件夹中的所有文件循环它?

如何使用Windows批处理文件或VBS脚本一次将多个文件从2个文件夹一次复制到另一个文件夹

如何一次将多个文件夹中存在的所有tar.gz文件提取到另一个目录?

如何对子文件夹中的所有图像使用ImageMagick转换命令

使用PowerShell脚本从所有30天以上的子文件夹中删除文件

在外壳程序脚本中使用*变量引用文件夹中的所有文件

如何使用Python连接文件夹中的所有csv文件?

如何查找使用Python在特定文件夹中今天创建的所有文件

如何使用python代码处理文件夹中的所有文件

如何使用python遍历多个文件夹中的所有文件

如何一次在python中为Windows创建多个文件夹

如何使用.gitignore忽略文件夹中的所有子文件夹

在Mac上使用Pandoc将文件夹中的所有文件转换为md

使用Python 2.7.5将文件夹中的所有压缩文件解压缩到同一文件夹

使用PHP从文件夹中删除所有文件?

如何使用一个命令将所有pdf文件转换为文本(在文件夹内)?

如何使用bash脚本在所有文件夹中放置文件副本?

如何使用os python获取另一个文件夹中文件的路径

如何使用Files.walk一次读取子目录中的所有文件?

如何使用 Python 读取文件夹中的所有 .txt 文件并将其内容附加到一个 .txt 文件中?

如何替换文件夹中Word文档中所有出现的字符串