python - 仅将 mp4 文件与 Pathlib 合并
问题描述
我只想知道这是否可能不使用shutil合并文件?但仅使用 pathlib。
我用于合并文件的旧代码:
def combine(source: str, output: str),
with open(output, 'wb') as output_file:
for file in iglob(os.path.join(source, "*.mp4")):
print(f'Merging', file, end='\r')
with open(file, 'rb') as input_file:
shutil.copyfileobj(input_file, output_file)
这是我尝试使用 pathlib
def combine(source: str, output: str),
for file in Path(source).glob('*.mp4'):
print(f'Merging', file)
Path(output).write_bytes(file.read_bytes())
问题是,它没有附加字节并组合到输出文件。
解决方案
您应该只打开一次输出文件,然后写入您读取的每个文件的字节。
def combine(source: str, output: str),
print(f'Creating {output}')
with open(Path(output), 'wb') as outfile:
for file in Path(source).glob('*.mp4'):
print(f'Appending {file}')
outfile.write(file.read_bytes())
推荐阅读
- javascript - webpack 抱怨找不到模块
- kubernetes - 从集群外在 kubernetes 上听 kafak
- jquery - 使用 jquery 无限滚动
- python-3.x - 彩虹动画文本
- javascript - 可以从 URL 中提取所有子域 + 域并与 RFC 3490 兼容的正则表达式
- c - 如果有的话,编码在什么时候开始在 C 中发挥作用?那么如何正确打印字符串呢?
- reactjs - NotFound 组件不在路由器中呈现
- firebase - 通过 REST 构建的 firebase 动态链接指定社交预览信息
- css - 将鼠标悬停在 ggplot 上以错误的顺序更新 CSS 和呈现工具提示
- reactjs - 无法重定向到搜索结果页面