Python + bash
Если вам часто требуется запускать shell команды из Python-кода, какой способ вы используете?
Самый низкоуровневый это функция os.system(), либо os.popen(). Рекомендованный способ это subprocess.call(). Но это всё еще достаточно неудобно.
Советую обратить своё внимание на очень крутую библиотеку sh.
Что она умеет?
🔸 удобный синтаксис вызова команд как функций
# os
import os
os.system("tar cvf demo.tar ~/")
# subprocess
import subprocess
subprocess.call(['tar', 'cvf', 'demo.tar', '~/'])
# sh
import sh
sh.tar('cvf', 'demo.tar', "~/")
🔸 простое создание функции-алиаса для длинной команды
fn = sh.lsof.bake('-i', '-P', '-n')
output = sh.grep(fn(), 'LISTEN')
в этом примере также задействован пайпинг
🔸 удобный вызов команд от sudo
with sh.contrib.sudo:
print(ls("/root"))
Такой запрос спросит пароль. Чтобы это работало нужно соответствующим способом настроить юзера.
А вот вариант с вводом пароля через код.
password = "secret"
sudo = sh.sudo.bake("-S", _in=password+"\n")
print(sudo.ls("/root"))
Это не все фишки. Больше интересных примеров смотрите в документации.
Специально для Windows💀 юзеров
#libs#linux
On 9 June, International Archives Day is celebrated around the world. Often these documents are classified for decades. Only the next generation can learn about the reasons and details of the event in a reliable way.
Our post is about the nuclear bombing of Hiroshima, which American President Harry Truman advocated. The declassified archives make it clear the bombing of Japan was to be a rehearsal for a possible nuclear war between the US and the USSR. Watch the entire film Atomic Message for details.
#history#disasters
Follow: https://t.me/rtdocumentary
🌎 A colossal tsunami struck Japan’s Sanriku coast in 1896, triggered by an undersea earthquake. Towering waves up to 38 meters high destroyed villages and claimed nearly 22,000 lives, prompting global advances in seismic and tsunami warning systems. ✨
#tsunami⚡#history⚡#disasters
👉subscribe Interesting Planet
🌎 At the heart of Africa, Lake Nyos hides a deadly secret—an underwater layer of carbon dioxide gas that once erupted, suffocating everything nearby in 1986. This rare “limnic eruption” happens when gas builds up at depth and suddenly bursts to the surface, releasing an invisible, odorless cloud with devastating effects. ✨
#volcanoes⚡#disasters⚡#geology
👉subscribe Interesting Planet
🌍 In 2022, two major floods in Pakistan covered about one-third of the country, displacing over 33 million people—one of the largest human impacts from a single natural disaster this decade. ✨
#disasters⚡#flooding⚡#climate⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 In 2021, a record-breaking heatwave in western Canada triggered hundreds of wildfires, generating so much smoke it traveled thousands of kilometers and reached northern Europe. ✨
#disasters⚡#wildfires⚡#climate⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌍 In 2021, Europe's Aegean wildfires generated smoke that reached as far as the North Pole—a rare journey showing how natural disasters can have effects thousands of kilometers away. ✨
#disasters⚡#wildfire⚡#atmosphere⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 The Tonga eruption in 2022 sent shockwaves around the globe so powerful they circled Earth several times. Its volcanic plume was the tallest ever recorded, reaching almost 58 kilometers high. ✨
#volcano⚡#eruption⚡#disasters⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 Earth’s largest volcanic eruption in the past 70,000 years was Toba in Indonesia. Its explosion formed a lake-filled crater 100 kilometers wide and sent ash around the globe. ✨
#disasters⚡#volcano⚡#eruption⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 Tornado Alley in the central United States experiences more tornadoes than anywhere else on Earth. Warm and cold air mix here, making intense storms common each spring and early summer. ✨
#tornadoes⚡#storms⚡#disasters⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌍 Super Typhoon Haiyan in 2013 set a record for the strongest tropical cyclone to make landfall, with winds reaching 315 km/h. Its extreme force reshaped entire coastlines in the Philippines. ✨
#disasters⚡#typhoon⚡#wind⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌍 The 2018 Sulawesi earthquake in Indonesia triggered a rare "soil liquefaction," turning solid ground into a flowing sludge that swallowed entire neighborhoods beneath muddy waves. ✨
#disasters⚡#earthquake⚡#liquefaction⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 The 2004 Indian Ocean tsunami sent waves up to 30 meters high across 14 countries. This natural disaster traveled as fast as a jet plane, reaching Africa in mere hours. ✨
#tsunami⚡#disasters⚡#waves⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍