首页 > 解决方案 > 将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'

标签: python

解决方案


正如用户建议的那样,subprocess认为您想将整个命令作为一个单一的东西执行,但失败了。

一个不错的选择是将subprocess.run字符串参数替换为列表:

# pass a list directly
stuff = sp.run(["cat", part1.name, part2.name, part3.name, part4.name, ">", path])

这对我有用。


推荐阅读