Python如何合并bilibili分段视频合并的视频文件

下次自动登录
现在的位置:
& 综合 & 正文
Python分割文件以及合并文件
用Python进行文件操作是比较简单的,在Python中file是内置类型之一,内置的函数open、file都可以创建file对象,创建好之后就可以对其进行读写等操作。
文件分割的原理很简单:以二进制形式打开文件流,按照指定的大小读取,然后写入新文件。文件合并的原理正好相反。
split_file.py
#!/usr/bin/python########################################################################### split a file join.py put# this is a customizable version of the standard unix split command-line # because it is written in Python, it also works on Windows and# ca because it exports a function, its logic can # also be imported and reused i##########################################################################
import sys, oskilobytes = 1024megabytes = kilobytes * 1000chunksize = int(1.4 * megabytes)
# default: roughly a floppy
def split(fromfile, todir, chunksize=chunksize):
if not os.path.exists(todir):
# caller handles errors
os.mkdir(todir)
# make dir, read/write parts
for fname in os.listdir(todir):
# delete any existing files
os.remove(os.path.join(todir, fname))
partnum = 0
input = open(fromfile, 'rb')
# use binary mode on Windows
# eof=empty string from read
chunk = input.read(chunksize)
# get next part &= chunksize
if not chunk: break
= partnum+1
filename = os.path.join(todir, ('part%04d' % partnum))
= open(filename, 'wb')
fileobj.write(chunk)
fileobj.close()
# or simply open().write()
input.close()
assert partnum &= 9999
# join sort fails if 5 digits
return partnum
if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == '-help':
print 'Use: split.py [file-to-split target-dir [chunksize]]'
if len(sys.argv) & 3:
interactive = 1
fromfile = raw_input('File to be split? ')
# input if clicked
= raw_input('Directory to store part files? ')
interactive = 0
fromfile, todir = sys.argv[1:3]
# args in cmdline
if len(sys.argv) == 4: chunksize = int(sys.argv[3])
absfrom, absto = map(os.path.abspath, [fromfile, todir])
print 'Splitting', absfrom, 'to', absto, 'by', chunksize
parts = split(fromfile, todir, chunksize)
print 'Error during split:'
print sys.exc_info()[0], sys.exc_info()[1]
print 'Split finished:', parts, 'parts are in', absto
if interactive: raw_input('Press Enter key') # pause if clicked
join_file.py
#!/usr/bin/python########################################################################### join all part files in a dir created by split.py, to recreate file.
# This is roughly like a 'cat fromdir/* & tofile' command on unix, but is # more portable and configurable, and exports the join operation as a # reusable function.
Relies on sort order of file names: must be same # length.
Could extend split/join to popup Tkinter file selectors.##########################################################################
import os, sysreadsize = 1024
def join(fromdir, tofile):
output = open(tofile, 'wb')
= os.listdir(fromdir)
parts.sort()
for filename in parts:
filepath = os.path.join(fromdir, filename)
= open(filepath, 'rb')
filebytes = fileobj.read(readsize)
if not filebytes: break
output.write(filebytes)
fileobj.close()
output.close()
if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == '-help':
print 'Use: join.py [from-dir-name to-file-name]'
if len(sys.argv) != 3:
interactive = 1
fromdir = raw_input('Directory containing part files? ')
= raw_input('Name of file to be recreated? ')
interactive = 0
fromdir, tofile = sys.argv[1:]
absfrom, absto = map(os.path.abspath, [fromdir, tofile])
print 'Joining', absfrom, 'to make', absto
join(fromdir, tofile)
print 'Error joining files:'
print sys.exc_info()[0], sys.exc_info()[1]
print 'Join complete: see', absto
if interactive: raw_input('Press Enter key') # pause if clicked
以上代码在window下面测试成功,可以分割文件文件已经任何二进制文件。
【上篇】【下篇】o(╥﹏╥)o
页面找不到了
推荐阅读:后使用快捷导航没有帐号?
只需一步,快速开始
请完成以下验证码
请完成以下验证码
主题帖子荣誉
新鱼友, 积分 59, 距离下一级还需 41 积分
新鱼友, 积分 59, 距离下一级还需 41 积分
查看: 1230|回复: 21
& 累计签到:44 天连续签到:1 天
马上注册加入鱼C,享用更多服务吧^_^
才可以下载或查看,没有帐号?
有a1.txt和a2.txt两个文档:
a1里的内容如下:
a2里的内容如下:
如何把a1和a2合并为a3,合并后a3内容如下:
abcdabcdefj
bcdaskdfjlas
cdabsldjflka
dabcbdsagg
& 累计签到:835 天连续签到:43 天
f1 = open('a1.txt', 'r')
f2 = open('a2.txt', 'r')
f3 = open('a3.txt', 'w')
l3 = [l1.rstrip('\n') + l2 for l1 in f1 for l2 in f2]
for each_line in l3:
& & f3.write(each_line)复制代码
不懂欢迎追问
& 累计签到:44 天连续签到:1 天
不懂欢迎追问
你好!好像不行,都是同一行的数据
& 累计签到:835 天连续签到:43 天
你好!好像不行,都是同一行的数据
那就在write后面加\r\n
& 累计签到:66 天连续签到:1 天
不懂欢迎追问
第五局那两个for是怎么个用法额
& 累计签到:835 天连续签到:43 天
第五局那两个for是怎么个用法额
列表解析式
老师讲过的哦~
& 累计签到:66 天连续签到:1 天
列表解析式
老师讲过的哦~
哈哈。懂啦,有点像lambda
& 累计签到:44 天连续签到:1 天
那就在write后面加\r\n
麻烦写一下格式,谢谢
& 累计签到:835 天连续签到:43 天
麻烦写一下格式,谢谢
这是学校的作业吗
f1 = open('a1.txt', 'r')
f2 = open('a2.txt', 'r')
f3 = open('a3.txt', 'w')
l3 = [l1.rstrip('\n') + l2 for l1 in f1 for l2 in f2]
for each_line in l3:
& & f3.write(each_line + '\r\n')
复制代码
& 累计签到:44 天连续签到:1 天
这是学校的作业吗
还是不行呢!都是同一行的数据
& 累计签到:835 天连续签到:43 天
还是不行呢!都是同一行的数据
你把同一行的数据贴上来我看看问题出在哪了
& 累计签到:44 天连续签到:1 天
你把同一行的数据贴上来我看看问题出在哪了
就是a1.txt都是相同的:
abcdabcdefj
abcdskdfjlas
abcdsldjflka
abcdbdsagg
& 累计签到:835 天连续签到:43 天
就是a1.txt都是相同的:
abcdabcdefj
这是已经分行了, 你是要分行还是不要?
难道我曲解你的意思了?
& 累计签到:44 天连续签到:1 天
这是已经分行了, 你是要分行还是不要?
难道我曲解你的意思了?
a1.txt文件中的数据只有第一行,a2.txt文件中的数据是每行都用到了
我想要的结果是:
abcdabcdefj
bcdaskdfjlas
cdabsldjflka
dabcbdsagg
你现在的结果是:
abcdabcdefj
abcdskdfjlas
abcdsldjflka
abcdbdsagg
& 累计签到:835 天连续签到:43 天
a1.txt文件中的数据只有第一行,a2.txt文件中的数据是每行都用到了
我想要的结果是:
abcdabcdefj
非常抱歉, 没看仔细
f1 = open('a1.txt', 'r').read().split('\n')
f2 = open('a2.txt', 'r').read().split('\n')
f3 = open('a3.txt', 'w')
l3 = [f1[i] + f2[i] for i in range(len(f1))]
for each_line in l3:
& & f3.write(each_line + '\r\n')
复制代码
& 累计签到:44 天连续签到:1 天
非常抱歉, 没看仔细
不行啊这次输出是空数据
& 累计签到:835 天连续签到:43 天
不行啊这次输出是空数据
a3.txt里面为空吗
& 累计签到:44 天连续签到:1 天
a3.txt里面为空吗
& 累计签到:835 天连续签到:43 天
你试试这段代码, 看看终端输出什么# f1 = open('a1.txt', 'r').read().split('\n')
# f2 = open('a2.txt', 'r').read().split('\n')
# f3 = open('a3.txt', 'w')
f1 = '''abcd
bcda
cdab
dabc'''.split('\n')
f2 = '''abcdefj
skdfjlas
sldjflka
bdsagg'''.split('\n')
l3 = [f1[i] + f2[i] for i in range(len(f1))]
for each_line in l3:
& & print(each_line)
& & # f3.write(each_line + '\r\n')复制代码
& 累计签到:44 天连续签到:1 天
a3.txt里面为空吗
可以了。谢谢!
小甲鱼强烈推荐
新的视频新的面貌,希望大家喜欢 (≧∇≦)ノ
- - - - - - - - - - - -
新课程,新体验!
移动客户端下载(未启用)
微信公众号
Powered by
Copyright &
&&& All Rights Reserved.Python实现分割文件及合并文件的方法_python
作者:用户
本文实例讲述了Python实现分割文件及合并文件的方法。分享给大家供大家参考。具体如下:分割文件split.py如下:#!/usr/bin/python########################################################################### split a file join.py ...
本文实例讲述了Python实现分割文件及合并文件的方法。分享给大家供大家参考。具体如下:
分割文件split.py如下:
#!/usr/bin/python
##########################################################################
# split a file join.py put
# this is a customizable version of the standard unix split command-line
# because it is written in Python, it also works on Windows and
# ca because it exports a function, its logic can
# also be imported and reused i
##########################################################################
import sys, os
kilobytes = 1024
megabytes = kilobytes * 1000
chunksize = int(1.4 * megabytes)
# default: roughly a floppy
def split(fromfile, todir, chunksize=chunksize):
if not os.path.exists(todir):
# caller handles errors
os.mkdir(todir)
# make dir, read/write parts
for fname in os.listdir(todir):
# delete any existing files
os.remove(os.path.join(todir, fname))
partnum = 0
input = open(fromfile, 'rb')
# use binary mode on Windows
# eof=empty string from read
chunk = input.read(chunksize)
# get next part &= chunksize
if not chunk: break
partnum = partnum+1
filename = os.path.join(todir, ('part%04d' % partnum))
fileobj = open(filename, 'wb')
fileobj.write(chunk)
fileobj.close()
# or simply open().write()
input.close()
assert partnum &= 9999
# join sort fails if 5 digits
return partnum
if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == '-help':
print 'Use: split.py [file-to-split target-dir [chunksize]]'
if len(sys.argv) & 3:
interactive = 1
fromfile = raw_input('File to be split? ')
# input if clicked
todir = raw_input('Directory to store part files? ')
interactive = 0
fromfile, todir = sys.argv[1:3]
# args in cmdline
if len(sys.argv) == 4: chunksize = int(sys.argv[3])
absfrom, absto = map(os.path.abspath, [fromfile, todir])
print 'Splitting', absfrom, 'to', absto, 'by', chunksize
parts = split(fromfile, todir, chunksize)
print 'Error during split:'
print sys.exc_info()[0], sys.exc_info()[1]
print 'Split finished:', parts, 'parts are in', absto
if interactive: raw_input('Press Enter key') # pause if clicked
合并文件join_file.py如下:
#!/usr/bin/python
##########################################################################
# join all part files in a dir created by split.py, to recreate file.
# This is roughly like a 'cat fromdir/* & tofile' command on unix, but is
# more portable and configurable, and exports the join operation as a
# reusable function. Relies on sort order of file names: must be same
# length. Could extend split/join to popup Tkinter file selectors.
##########################################################################
import os, sys
readsize = 1024
def join(fromdir, tofile):
output = open(tofile, 'wb')
parts = os.listdir(fromdir)
parts.sort()
for filename in parts:
filepath = os.path.join(fromdir, filename)
fileobj = open(filepath, 'rb')
filebytes = fileobj.read(readsize)
if not filebytes: break
output.write(filebytes)
fileobj.close()
output.close()
if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == '-help':
print 'Use: join.py [from-dir-name to-file-name]'
if len(sys.argv) != 3:
interactive = 1
fromdir = raw_input('Directory containing part files? ')
tofile = raw_input('Name of file to be recreated? ')
interactive = 0
fromdir, tofile = sys.argv[1:]
absfrom, absto = map(os.path.abspath, [fromdir, tofile])
print 'Joining', absfrom, 'to make', absto
join(fromdir, tofile)
print 'Error joining files:'
print sys.exc_info()[0], sys.exc_info()[1]
print 'Join complete: see', absto
if interactive: raw_input('Press Enter key') # pause if clicked
希望本文所述对大家的Python程序设计有所帮助。
以上是互联网用户为您的的内容,在阿里云内部有更多的关于Python实现分割文件及合并文件的方法_python的内容,欢迎继续使用右上角搜索按钮进行搜索python、分割文件、合并文件、以便于您获取更多的相关信息。
本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:zixun-group@service.aliyun.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。
若您要投稿,删除文章请联系邮箱:zixun-group@service.aliyun.com
工作人员会在5个工作日内回复
脚本栏目为您免费提供
相关信息,包括
的信息 ,所有
相关内容均不代表阿里云的意见!投稿、删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员5个工作日内回复。没有更多推荐了,
不良信息举报
举报内容:
python合并ts视频
举报原因:
原文地址:
原因补充:
最多只允许输入30个字
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!

我要回帖

更多关于 网页分段视频合并下载 的文章

 

随机推荐