pythonyupyqt5合并bin文件

news/2024/7/16 7:51:31 标签: pycharm, ide, python

可以使用 Python 的 os 模块来合并多个二进制文件。首先,需要使用 os.path.getsize() 函数获取每个文件的大小,然后使用 os.open() 函数打开每个文件,并使用 os.read() 函数读取文件内容。最后,可以使用 Python 的 bytes 类型将文件内容进行拼接,然后使用 os.write() 函数将拼接后的二进制数据写入一个新的文件中。

以下是一个示例代码,它实现了将多个文件合并为一个新文件的功能:

import os# 待合并的文件列表
file_list = ['file1.bin', 'file2.bin', 'file3.bin']

# 计算合并后文件的大小
total_size = sum(os.path.getsize(f) for f in file_list)

# 创建一个空的 bytes 对象,用于保存合并后的二进制数据
merged_data = bytes(total_size)

# 拼接文件内容
offset = 0
for f in file_list:
    with open(f, 'rb') as file:
        data = file.read()
        merged_data[offset:offset+len(data)] = data
        offset += len(data)

# 将合并后的数据写入新文件
with open('merged_file.bin', 'wb') as file:
    file.write(merged_data)

在上面的代码中,file_list 变量中保存了待合并的文件列表,total_size 变量保存了合并后的文件的大小,merged_data 变量保存了合并后的二进制数据。


http://www.niftyadmin.cn/n/985266.html

相关文章

python中计算1_计算文件python中的所有1

使用collections.Counter:from collections import Counterwith open(abc.txt) as f:c Counter(int(line.split(None, 1)[0]) for line in f)print c输出:Counter({0: 2, -1: 2, 1: 1})这里str.split(None,1)只拆分一次:>>> s &qu…

杭电1385--Minimum Transport Cost(Floyd字典序打印路径)

题目:http://acm.hdu.edu.cn/showproblem.php?pid1385 InputFirst is N, number of cities. N 0 indicates the end of input.The data of path cost, city tax, source and destination cities are given in the input, which is of the form:a11 a12 ... a1Na21…

linux jdk配置多个_Linux起步系列(三):安装JDK及配置

一:背景日常工作中经常在服务器环境中进行相关设置和部署,对于有些工具的使用及功能验证需要Linux 环境且需要学习Linux相关知识,因此记录自己学习Linux的点点滴滴,并形成系列文章本文为系列第三篇:安装JDK及配置系列链…

JSF 2 radio buttons example

In JSF, “h:selectOneRadio” tag is used to render a set of HTML input element of type “radio“, and format it with HTML table and label tag. //JSF... <h:selectOneRadio value"#{user.favColor1}"><f:selectItem itemValue"Red" ite…

mysql 监听命令_mysql常用监控脚本命令列表

#/bin/sh#检测mysql server是否正常提供服务mysqladmin -u sky -ppwd -h localhost ping#获取mysql当前的几个状态值mysqladmin -u sky -ppwd -h localhost status#获取数据库当前的连接信息mysqladmin -u sky -ppwd -h localhost processlist#获取当前数据库的连接数mysql -u …

pluribus算法_E pluribus unum – OpenStack Swift清单对象

pluribus算法By default, the content of an OpenStack Swift object cannot be greater than 5 GB. However, you can use a number of smaller objects to construct a large object via the concept of segmentation. From OpenStack Large Object Support, “Segments of t…

mysql删除用户userdel_马哥网络教育班第21期+第三周课程练习

1、列出当前系统上所有已经登录的用户的用户名&#xff0c;注意&#xff1a;同一个用户登录多次&#xff0c;则只显示一次即可。[sihualocalhost ~]$ who | cut -d -f1 | uniqrootsihua2、取出最后登录到当前系统的用户的相关信息。[sihualocalhost ~]$ w | tail -1 | awk {pr…