Как разделить строку с 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
Cholesterol protects the integrity of cell membranes and keeps cells healthy and strong. If a person’s cholesterol level were 0 (an impossibility), his cell membranes would be dry and cracked and all the cell content would leak out.
@googlefactss#humanbody
For most women (about 65%), the left breast is bigger than the right one. Researchers are unsure why, but they point to hormonal influences during development.
@googlefactss#humanbody
A human spinal cord stops growing in length around age four or five, while the vertebral column (spine) continues to grow until late adolescence or early adulthood.
@googlefactss#humanbody
In a condition called pelvic prolapse, a woman’s vagina can literally fall out and hang between the legs. Pelvic prolapse, however, can often be fixed..
@googlefactss#humanbody
[read more...]
The same skin cells that make up a human vagina are the same type of cells that are in a human mouth. The nonkeratinized stratified squamous epithelium are types of mucous membranes that are sensitive, absorbent, and continuously shed and regenerated.
@googlefactss#humanbody
It is common and normal for mothers to poop while giving birth. This often happens during the pushing stage because the muscles used to push a baby out are the same ones used to have a bowel movement.
@googlefactss#humanbody#nowyouknow
Occasionally, human corpses can undergo saponification, a process in which body fat transforms into adipocere through a chemical reaction. Adipocere has a consistency similar to semi-hard cheese and possesses a soapy feel. The most well-known example of this phenomenon is the "Soap Lady," a 140-year-old mummy showcased in Pennsylvania.
@googlefactss#humanbody#nowyouknow
Myth: The tongue is divided up into specific areas that are solely responsible for sensing certain tastes (e.g., sweet at the tip, bitter at the back)
Fact: The tongue's receptor cells that identify the molecules underlying the basic tastes (sweet, sour, salt, bitter and umami) are distributed on taste buds all over the tongue although some areas may be slightly more sensitive to certain flavors..
@googlefactss#humanbody#nowyouknow#mythbusted
[read more...]
Fingernail facts:
The middle finger’s nail grows the fastest while the smallest finger’s grows the slowest.
Nails usually grow faster on your dominant or more active hand (more use → more repair → more blood/nutrients) and speed up in summer thanks to better circulation.
Toenails grow slower.
Overall, males’ nails often grow faster than females’, but during puberty and pregnancy higher hormone levels make nails grow quicker in women.
[Source]
@googlefactss
#FunFact#HumanBody#nails