Как разделить строку с 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
#newtgingrich : RT @RealAmVoice: "It is very clear that Putin has made a gamble — which is the gamble probably of his life."
@NewtGingrich joins the War Room to break down the cyber attacks and China displacing the United States.
Watch more War Room with #SteveBannon here: https://t.co/No6fL5Gjrqhttps://t.co/IqpJjfrwIf
Economic war with Russia on the horizon?
Steve Bannon:
“You’ve now got a major economic war that is going to kick up with Russia. They just put out the word. They said ‘Hey […] if you touch the $300 billion that you stole from us, each country that does it is going to answer to us personally.’ […] This is something we didn’t do to the Nazis, something we didn’t do to Imperial Japan… We gotta be very careful. Everybody is treading on thin ice on this one.”
#SteveBannon#Russia#war
👂More on Trump's Ear ⚠️
According to Steve Bannon, the United Kingdom is on the brink of civil war. Its leaders have allowed the country to be taken over, its institutions of power have been hollowed out, and those who resist are being punished. This is a battle for Western civilization.
This is why Donald Trump is not in London — because there are huge riots down there.
#SteveBannon#UK#civilwar
👂More on Trump's Ear ⚠️
January 6 panel moves to hold Steve Bannon in contempt
A U.S. congressional committee probing the deadly Jan. 6 assault on the Capitol said it would vote next week to hold Steve Bannon, a longtime adviser to former President Donald Trump, in contempt of Congress for refusing to comply with a subpoena.
#January6#Trump#SteveBannon#News#Reuters
Subscribe: http://smarturl.it/reuterssubscribe
Reuters brings you the latest business, finance and breaking news video from around the globe. Our reputation for accuracy and impartiality is unparalleled.
Get the latest news on: http://reuters.com/
Follow Reuters on Facebook: https://www.facebook.com/Reuters
Follow Reuters on Twitter: https://twitter.com/Reuters
Follow Reuters on Instagram: https://www.instagram.com/reuters/?hl=en
➖@reutersworldchannel➖
October 15, 2021: Bill Clinton, Steve Bannon, FDA Moderna booster, Texas abortion law, Immigration
Top headlines on October 15, 2021: Bill Clinton in hospital, Steve Bannon, FDA Moderna booster, Texas abortion law, Biden's immigration plans
1. Ex-President Bill Clinton is recovering in hospital after being treated ‘for a non-COVID-19 infection.’ The 75-year-old was admitted on Tuesday evening.
2. A panel probing the U.S. Capitol assault will vote to hold Steve Bannon in contempt of Congress after the Trump advisor refused to comply with a subpoena.
3. U.S. FDA advisers voted in favor of booster shots of the Moderna vaccine for Americans aged 65+ and those at high risk of illness.
A U.S. appeals court rulesd to keep a Texas abortion ban in effect. The law bars the procedure as early as six weeks into pregnancy.
5. U.S. President Joe Biden's administration will restart Trump’s ‘Remain in Mexico’ policy by mid-November. The program forces asylum seekers to wait in Mexico for U.S. court hearings
#FDA
#COVID19
#USBorder
#Texas
#abortion
#SteveBannon
#CapitolRiot
#BillClinton
#Clinton
#Moderna
#Vaccine
#Booster
#US
#Mexico
#Migrants
#Biden
#UnitedStates
#Trump
#News
#Reuters
Subscribe: http://smarturl.it/reuterssubscribe
Reuters brings you the latest business, finance and breaking news video from around the globe. Our reputation for accuracy and impartiality is unparalleled.
Get the latest news on: http://reuters.com/
Follow Reuters on Facebook: https://www.facebook.com/Reuters
Follow Reuters on Twitter: https://twitter.com/Reuters
Follow Reuters on Instagram: https://www.instagram.com/reuters/?hl=en
➖@reutersworldchannel➖