Как разделить строку с 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
🎩Agent 301 Puzzle | Sept 6
🧩Cards in Order:11:10:7:13
⚠️ Follow the sequence above to match perfectly!
❓How to Play:
1. Open @Agent301
2. Go to “Games” → “Puzzle”
3. Enter the combo and win!
🔥 Good luck and have fun!
🔔Subscribe
👉Join Agent301
➖➖➖➖➖➖➖➖➖➖
#Agent301#DailyPuzzle
🎩Agent 301 Puzzle | Sept 4
🧩Cards in Order:13:16:8:1
⚠️ Follow the sequence above to match perfectly!
❓How to Play:
1. Open @Agent301
2. Go to “Games” → “Puzzle”
3. Enter the combo and win!
🔥 Good luck and have fun!
🔔Subscribe
👉Join Agent301
➖➖➖➖➖➖➖➖➖➖
#Agent301#DailyPuzzle
🎩Agent 301 Puzzle | Sept 2
🧩Cards in Order:5:9:13:1
⚠️ Follow the sequence above to match perfectly!
❓How to Play:
1. Open @Agent301
2. Go to “Games” → “Puzzle”
3. Enter the combo and win!
🔥 Good luck and have fun!
🔔Subscribe
👉Join Agent301
➖➖➖➖➖➖➖➖➖➖
#Agent301#DailyPuzzle
🎩Agent 301 Puzzle | Sept 1
🧩Cards in Order:15:4:12:13
⚠️ Follow the sequence above to match perfectly!
❓How to Play:
1. Open @Agent301
2. Go to “Games” → “Puzzle”
3. Enter the combo and win!
🔥 Good luck and have fun!
🔔Subscribe👉Join Agent301
➖➖➖➖➖➖➖➖➖➖
#Agent301#DailyPuzzle
🎩Agent 301 Puzzle | Aug 31
🧩Cards in Order:8:13:1:4
⚠️ Follow the sequence above to match perfectly!
❓How to Play:
1. Open @Agent301
2. Go to “Games” → “Puzzle”
3. Enter the combo and win!
🔥 Good luck and have fun!
🔔Subscribe👉Join Agent301
➖➖➖➖➖➖➖➖➖➖
#Agent301#DailyPuzzle
🎩Agent 301 Puzzle | Aug 30
🧩Cards in Order:6:11:5:10
⚠️ Follow the sequence above to match perfectly!
❓How to Play:
1. Open @Agent301
2. Go to “Games” → “Puzzle”
3. Enter the combo and win!
🔥 Good luck and have fun!
🔔Subscribe
👉Join Agent301
➖➖➖➖➖➖➖➖➖➖
#Agent301#DailyPuzzle
🎩Agent 301 Puzzle | Aug 29
🧩Cards in Order:5:14:10:12
⚠️ Follow the sequence above to match perfectly!
❓How to Play:
1. Open @Agent301
2. Go to “Games” → “Puzzle”
3. Enter the combo and win!
🔥 Good luck and have fun!
🔔Subscribe
👉Join Agent301
➖➖➖➖➖➖➖➖➖➖
#Agent301#DailyPuzzle
🎩Agent 301 Puzzle | Aug 27
🧩Cards in Order:2:3:10:5
⚠️ Follow the sequence above to match perfectly!
❓How to Play:
1. Open @Agent301
2. Go to “Games” → “Puzzle”
3. Enter the combo and win!
🔥 Good luck and have fun!
🔔Subscribe
👉Join Agent301
➖➖➖➖➖➖➖➖➖➖
#Agent301#DailyPuzzle
🎩Agent 301 Puzzle | Aug 26
🧩Cards in Order:12:6:5:13
⚠️ Follow the sequence above to match perfectly!
❓How to Play:
1. Open @Agent301
2. Go to “Games” → “Puzzle”
3. Enter the combo and win!
🔥 Good luck and have fun!
🔔Subscribe
👉Join Agent301
➖➖➖➖➖➖➖➖➖➖
#Agent301#DailyPuzzle
🎩Agent 301 Puzzle | Aug 24
🧩Cards in Order:5:15:11:4
⚠️ Follow the sequence above to match perfectly!
❓How to Play:
1. Open @Agent301
2. Go to “Games” → “Puzzle”
3. Enter the combo and win!
🔥 Good luck and have fun!
🔔Subscribe
👉Join Agent301
➖➖➖➖➖➖➖➖➖➖
#Agent301#DailyPuzzle
🎩Agent 301 Puzzle | Aug 23
🧩Cards in Order:3:5:2:12
⚠️ Follow the sequence above to match perfectly!
❓How to Play:
1. Open @Agent301
2. Go to “Games” → “Puzzle”
3. Enter the combo and win!
🔥 Good luck and have fun!
🔔Subscribe
👉Join Agent301
➖➖➖➖➖➖➖➖➖➖
#Agent301#DailyPuzzle
🎩Agent 301 Puzzle | Aug 21
🧩Cards in Order:8:16:13:4
⚠️ Follow the sequence above to match perfectly!
❓How to Play:
1. Open @Agent301
2. Go to “Games” → “Puzzle”
3. Enter the combo and win!
🔥 Good luck and have fun!
🔔Subscribe
👉Join Agent301
➖➖➖➖➖➖➖➖➖➖
#Agent301#DailyPuzzle