python - 将4个下载的文件合并为一个
问题描述
我目前有 4 个文件,我使用 linux 命令split
将它们分成 50 meg 文件。
我目前正在尝试这个,但它告诉我它找不到文件。
import requests
import tempfile
import os
import subprocess as sp
def download_files_from_github(path, model_name):
if model_name == "u2net":
part1 = tempfile.NamedTemporaryFile(delete=False)
part2 = tempfile.NamedTemporaryFile(delete=False)
part3 = tempfile.NamedTemporaryFile(delete=False)
part4 = tempfile.NamedTemporaryFile(delete=False)
try:
part1_content = requests.get('https://github.com/nadermx/backgroundremover/raw/main/models/u2aa')
part1.write(part1_content.content)
part1.close()
part2_content = requests.get('https://github.com/nadermx/backgroundremover/raw/main/models/u2ab')
part2.write(part2_content.content)
part2.close()
part3_content = requests.get('https://github.com/nadermx/backgroundremover/raw/main/models/u2ac')
part3.write(part3_content.content)
part3.close()
part4_content = requests.get('https://github.com/nadermx/backgroundremover/raw/main/models/u2ad')
part4.write(part4_content.content)
part4.close()
stuff = sp.run('cat %s %s %s %s > %s' % (part1.name, part2.name, part3.name, part4.name, path))
print(stuff)
finally:
os.remove(part1.name)
os.remove(part2.name)
os.remove(part3.name)
os.remove(part4.name)
download_files_from_github('~/.u2net/u2net.pth', 'u2net')
我得到这个错误
$ python tests.py
Traceback (most recent call last):
File "tests.py", line 34, in <module>
download_files_from_github('~/.u2net/u2net.pth', 'u2net')
File "tests.py", line 25, in download_files_from_github
stuff = sp.run('cat %s %s %s %s > %s' % (part1.name, part2.name, part3.name, part4.name, path))
File "/usr/lib/python3.6/subprocess.py", line 423, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'cat /tmp/tmp28877_uq /tmp/tmpx2t9s9we /tmp/tmpj4g8ahhw /tmp/tmpty1x7pjv > ~/.u2net/u2net.pth': 'cat /tmp/tmp28877_uq /tmp/tmpx2t9s9we /tmp/tmpj4g8ahhw /tmp/tmpty1x7pjv > ~/.u2net/u2net.pth'
解决方案
正如用户建议的那样,subprocess
认为您想将整个命令作为一个单一的东西执行,但失败了。
一个不错的选择是将subprocess.run
字符串参数替换为列表:
# pass a list directly
stuff = sp.run(["cat", part1.name, part2.name, part3.name, part4.name, ">", path])
这对我有用。
推荐阅读
- bash - 将具有特殊字符的变量字符串存储到bash中的数组中
- ionic-framework - 如何在 Ionic 4 上的本地存储中存储图像?(简单且最新)
- javascript - 如何在 index.js 中动态导出组件?
- mysql - 基于存在于另一个表中的条件选择值?
- java - 如何通过 JavaFX 从 Mariadb 数据库中获取数据到表中?
- c++ - 调用加入时如何修复“终止调用而没有活动异常”
- tomcat9 - 找不到位于 WEB-INF/lib jar 中 META-INF/resources 目录内的资源
- excel - 如何将两个私有子 Worksheet_Change(ByVal Target As Range) 合并到一个工作表中?
- selenium - 如何使用 mvn robotframework 运行单个测试用例?
- reactjs - React Navigation 支持 useEffect Hook?错误回调不是函数