我目前有 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'
uj5u.com熱心網友回復:
正如用戶所建議的,subprocess認為您想將整個命令作為一個單一的事情執行,但失敗了。
一個不錯的選擇是用subprocess.run串列替換字串引數:
# pass a list directly
stuff = sp.run(["cat", part1.name, part2.name, part3.name, part4.name, ">", path])
這對我有用。
uj5u.com熱心網友回復:
嘗試改變
stuff = sp.run('cat %s %s %s %s > %s' % (part1.name, part2.name, part3.name, part4.name, path))
到
stuff = sp.run(f'cat {part1.name} {part2.name} {part3.name} {part4.name} > {path}'.split())
您需要將串列傳遞給 sp.run,而不是字串。我所做的基本上是創建執行字串,然后將其拆分為命令和引數串列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/324944.html
標籤:Python
