python十六进制转pcap文件_使用必需的列python将所有pcap文件转换为csv

在Python中,你需要将所有pcap文件转换为CSV并保持与原始子目录相同的结构。当尝试创建输出目录时遇到了TypeError。解决方案是使用os.makedirs()确保子目录存在,并使用os.path.splitext()替换文件扩展名。通过这样做,你可以正确地生成CSV文件并保持目录结构。
摘要由CSDN通过智能技术生成

I need to write all the output CSV files to a different folder. For example if .pcap files were in subfolders Sub1, Sub2. And Sub1 has a1.pcap and a2.pcap. Sub2 has b1.pcap and b2.pcap.

I need my output CSV files to get written into a folder with the same names as above. Sub1, Sub2, then Sub1 should have a1.csv, a2.csv. Sub2 should have b1.csv, b2.csv.

How can I do that please?

I am getting the error below:

outputdir = startdir / "Outcsv"

TypeError: unsupported operand type(s) for /: 'str' and 'str'

The code is:

import os

startdir= '/root/Desktop/TTT'

suffix= '.pcap'

outputdir = startdir / "Outcsv"

for root,dirs, files, in os.walk(startdir):

for name in files:

if name.endswith(suffix):

filename = os.path.join(root,name)

output_filename = outputdir / filename.relative_to(startdir)

cmd = 'tshark -r {} -T fields -e frame.number -e frame.time_relative -e wlan.sa -e wlan.da -e wlan.ta -e wlan.ra -e frame.time_delta_displayed -e frame.len -E header=y -E separator=, -E quote=d -E occurrence=f > {}.csv'

final_cmd = cmd.format(filename, output_filename)

os.system(final_cmd)

解决方案

If you are trying to recreate a folder structure at a different location you will need to ensure that the folders are created. This can be done using the os.makedirs() command. The subfolder structure can be determined by using any path deeper than startdir. This can then be appended onto your outputdir location.

The file extension can also be replaced by using os.path.splitext().

For example:

import os

startdir = '/root/Desktop/TTT'

suffix= '.pcap'

outputdir = os.path.join(startdir, "Outcsv")

for root, dirs, files, in os.walk(startdir):

for name in files:

if name.lower().endswith(suffix):

sub_folders = root[len(startdir)+1:]

input_filename = os.path.join(root, name)

output_path = os.path.join(outputdir, sub_folders)

os.makedirs(output_path, exist_ok=True) # Ensure the output folder exists

output_filename = os.path.join(output_path, os.path.splitext(name)[0] + '.csv')

cmd = 'tshark -r {} -T fields -e frame.number -e frame.time_relative -e wlan.sa -e wlan.da -e wlan.ta -e wlan.ra -e frame.time_delta_displayed -e frame.len -E header=y -E separator=, -E quote=d -E occurrence=f > {}'

final_cmd = cmd.format(input_filename, output_filename)

print(final_cmd)

os.system(final_cmd)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值