Как разделить строку с shell-командой на отдельные аргументы в виде списка?
Если сделать просто сплит по пробелу то получим то что надо, кроме случаев со вставками текста с пробелами. Например так:
>>> '-arg "I Am Groot"'.split(' ')
['-arg', '"I', 'Am', 'Groot"']
Чтобы учитывать текст в кавычках как единый аргумент можно воспользоваться функцией shlex.split()
Кто читает мой канал давно, уже в курсе.
А что делать, если нужно обратное действие? Объединить аргументы из списка в строку и при этом добавить кавычки в аргумент с пробелами.
Конечно, если вы используете subprocess то он сам всё разрулит. Но если вам нужна именно команда одной строкой, то можно воспользоваться готовой функцией в том же subprocess.
>>> from subprocess import list2cmdline
>>> list2cmdline(['-arg', 'I Am Groot'])
'-arg "I Am Groot"'
Он также позаботится об экранировании уже имеющихся кавычек
>>> list2cmdline(['-arg', 'I Am "Groot"'])
'-arg "I Am \"Groot\""'
А вот так он может "схлопнуть" в команду JSON
>>> list2cmdline(['--json', json.dumps({'key': 'value'})])
'--json "{\"key\": \"value\"}"'
_______________
Возможно кто-то спросит, а зачем соединять аргументы в строку если subprocess сам это сделает а os.system не наш путь?
Мне как-то потребовалось отправлять команду на удалённое выполнение и в API поддерживалось указание команды только строкой. Так что всякое бывает)
#libs#basic
🚀🚀 Storming Profits in the Premium Group
🎯🎯#DEEP/USDT has covered all the targets to give a profit 106% for all Premium Members
👁🗨Contact @futurechief to enter the Premium Futures & SPOT Group for daily gains
🚀🚀 123% Profit on #DEEP/USDT for our Premium Members on Binance Futures/Bybit/OKXX/Kucoin/Bitget
✅✅ All target completed
👁🗨Contact @primemod to enter the Premium Group for high quality SPOT & FUTURES Signals
Don't avoid the cliches - they always work because they are clichés.
In this post, you may face the author's offensive views about people's excessive repeated utterances that are widespread, calling attention to avert them. Basically, let it be on Instagram stories or Facebook profile pictures, you see people who post a picture of Yale, write Yale class of 2029 on the bio, and many similar actions.
Holders could have no issue with posting them, and they are entitled to publish anything they care to do unless they don't publish political statements that can be conceived as an opposing point. By talking with people who already know the ups and downs of US admissions and hearing things I wasn't aware of, I realized that getting into a TOP-20 school requires hard work: nighttime study sessions, countless drafts of the final masterpiece, and dreadful deadlines. But everyone, not just me, can sense that people ignore the pain of those mentioned above and end up somewhere else where they have no desire to pursue their 4-year life.
Rather than writing cool bio captions for NYU'28 or whichever college you dream of, spend time working on crucial components of the college application, check your essays to the expert, scrutinize what actions "99%" applicants are doing, and do the opposite or a bizarreness. My utterances may hurt souls, but I'm showing the truth (ridiculous actions).
Now, get off this app and finalize your college list, which you care less to do. Two months left to catch the golden fish!
#deep
#Deep Research #分享
分享:三款OpenAI Deep Research替代开源项目
https://github.com/nickscamara/open-deep-research
https://github.com/mshumer/OpenDeepResearcher
https://github.com/dzhng/deep-research
📢频道✈️群组☘️博客📬投稿
Happiness always look small while you hold it in your hands,
but let it go,
and you will learn at once
how big and precious it is.
– Maxim Gorky
#happiness#deep@quietworld🍃