Заметка начинающим, которые часто сталкиваются с подобной непоняткой.
Ситуация следующая, есть список файлов:
names = [
'image.bmp',
'second.txt.bkp',
'data.db',
'.config.cfg',
'file.ext.bkp'
]
И мы хотим убрать у них окончание ".bkp".
Не знаю зачем, пример довольно надуманный) Но суть он показывает, а это главное.
Те, кто еще не очень знаком с библиотекой os.path или pathlib, вероятно решат обработать имена как строки. И тут вполне подойдет метод строки strip().
Что делает этот метод? Он отрезает указанные символы по обеим сторонам строки. Если ничего не указать, то убирает невидимые символы (пробелы, табуляции и переносы строк).
В нашем случае будет выглядеть вот так:
>>> name.strip('.bkp')
То есть просим удалить строку '.bkp' по краям имени файла, если таковая есть.
Можно применить аналогичный метод rstrip(), чтобы отрезать только справа, но для этого примера используем обычный.
>>> for name in names:
>>> print(name.strip('.bkp'))
image.bm
second.txt
data.d
config.cfg
file.ext
Хм, что-то не то с нашими именами! Что случилось??? Видим нежелательное переименование в именах, где и близко не было указанной строки '.bkp'
А дело всё в том, что данный метод ищет не указанную строку, а указанные символы, и не важно в каком порядке.
Для метода strip() строка '.bkp' это не паттерн для поискаа список символов. Потому он отрезал симовол 'p' от '.bmp' и удалил точку из файла '.config.cfg'.
Как тогда правильно заменить именно паттерн? Для начинающего можно посоветовать метод строки replace(), который как раз использует для замены указанную строку целиком. В нашем примере заменим её на пустую строку.
>>> for name in names:
>>> print(name.replace('.bkp', ''))
image.bmp
second.txt
data.db
.config.cfg
file.ext
Уже лучше, но помните, это лишь пример про strip(). Для работы с именами файлов есть способы и более "правильные", дающие однозначно верный результат. Я взял файлы только в качестве примера. Даже replase() тут может сделать не то что ожидаем.
Просто впредь будьте внимательны с этим strip().
#basic
🚗💨 Introducing the track-ready Cadillac CT5-V! 🎉
This powerful beast now features a Precision Package with enhanced suspension, including increased stiffness and reprogrammed adaptive dampers for an exhilarating ride on the track! 🏁✨
🔧 Key Highlights:
- Carbon-Ceramic Brakes for superior stopping power 🛑
- 6.2L V8 Engine producing an impressive 677 hp and 892 Nm of torque 💪🔥
- RWD for that ultimate driving experience!
Get ready to unleash the power! 💥
#CadillacCT5V#PrecisionPackage#TrackReady#V8Power#Auto
🚗✨Lexus Unveils Upgraded LC Coupe and Convertible!✨🚗
Since its debut in 2017, the Lexus LC family has received a sleek exterior makeover and new interior materials. But that's not all! The updated models now feature a stiffer chassis, thanks to additional braces installed under the floor near the front and rear axles, enhancing overall handling. 🏎️💨
Under the hood, the Lexus LC offers a powerful lineup: a 5.0L V8 engine producing 477 hp or a hybrid V6 powertrain with a 3.5L engine delivering 359 hp. 🔥🔋
#LexusLC#LuxuryCars#CarEnthusiast#DrivingExperience#AutoNews#V8Power#Auto
🌟 Exciting News for Supercar Enthusiasts! 🚗💨
The British have officially kicked off production of the stunning Ginetta Akula! 🦈✨
First unveiled in 2019, this masterpiece is limited to just 20 units, starting at £275,000. 💰
Featuring a classic design, the Akula boasts a powerful 6.4L V8 engine with 600 hp, paired with a manual transmission and rear-wheel drive. 🔥 Despite its robust engine, it weighs only 1.2 tons, accelerating from 0 to 100 km/h in just 2.9 seconds! ⚡️🏁 With a top speed of 290 km/h, this beauty is ready to roar!
Are you ready to experience the thrill? 😍
#GinettaAkula#Supercar#LimitedEdition#V8Power#CarEnthusiast#LuxuryCars#SpeedDemon#Auto
🚗💨 Introducing the BMW M5 Touring! 💨🚗
BMW has officially unveiled the stunning M5 in Touring body style!🌟
Under the hood, it boasts a powerful V8 twin-turbo engine with an impressive total output of 727 horsepower!🔥💪
With the rear seats folded down, you can fit a whopping 1.6 cubic meters of cargo! 🧳📦 And it can sprint from 0 to 100 km/h in just 3.6 seconds!⏱️⚡️
Weighing in at 2508 kg, this beauty is slightly heavier than its sedan counterpart. 🚙✨ This is only the third generation of the M5 in this body style and the first to hit the US market! 🇺🇸
Prices start at $121,500.💵
Get ready for an exhilarating ride! 🏁
#BMW#M5Touring#SportsCar#V8Power#LuxuryPerformance#CarEnthusiast#FastCars#Auto#BMWM🚘🌍
🚗💨 Dodge Unveils the "Farewell" Durango SRT Hellcat Silver Bullet! 💨🚗
This stunning special edition is one of the last to feature the powerful 6.2-liter HEMI V8 engine. As we bid farewell to these muscle machines, the Durango will soon lose its mighty motors. 😢💔
The Silver Bullet edition stands out with its exclusive decor and rich standard features, starting at $113,325**—about **$11,000 more than the previous year's base Durango Hellcat! 💰✨
Get ready to say goodbye to an icon!
#Dodge#DurangoSRT#Hellcat#SilverBullet#MuscleCar#Auto#V8Power#SUV🏁🔥
🚗💨 Introducing the Naran Hypercar: The Ultimate Powerhouse! 🌟
Get ready for the unveiling of the Naran Hypercar in the UK this August! 🇬🇧 With a staggering 1000 horsepower, this beast promises to be the fastest four-seater car in the world, powered by a traditional internal combustion engine—no electric enhancements! ⚡️❌
Only 39 units will be produced, making it an exclusive masterpiece! 🎨✨
Under the hood, it features a 5.0-liter twin-turbo V8, derived from BMW's impressive 4.4-liter engine. 🏎️💥 And yes, it comes with full-wheel drive for that ultimate driving experience! 🌍🚀
#NaranHypercar#CarEnthusiasts#AutomotiveExcellence#V8Power#Hypercar#FastestCar#LuxuryCars#CarLovers#InnovativeEngineering
🚗💨 Introducing the Ford F-150 FP700S! 🎉
Get ready for the ultimate asphalt beast! 🔥 With a powerful 700-horsepower V8 engine, this custom pickup is designed for performance and style.
🔧 Key Features:
• Supercharged 5.0L V8 with Whipple blower 💥
• Sporty exhaust system for that perfect roar 🎶
• Lowered suspension with new shocks for enhanced handling 🏎️
• Stunning 22-inch wheels for a bold look 🛞
• Aerodynamic body elements to slice through the air ✈️
#FordF150#FP700S#CustomGarage#PerformanceTruck#V8Power#AsphaltBeast#FordTuning#Auto
🚗✨ Exciting News from Mercedes-Maybach! ✨🚗
Get ready for the debut of the stunning new convertible on August 15th at the prestigious Pebble Beach! 🌊🏖️
The highly anticipated model has been teased since 2022, and we can't wait to see it in all its glory! 🤩
🔍 Fuel Efficiency: The Mercedes-Maybach SL boasts an impressive fuel consumption of 13.7 liters per 100 km*, closely matching the non-hybrid Mercedes-AMG SL 63 with its powerful 4.0L V8 engine, delivering a thrilling 585 hp and 800 Nm* of torque! 💨🔥
#MercedesMaybach#Convertible#LuxuryCars#PebbleBeach#CarDebut#FuelEfficiency#AMG#V8Power#Auto
🚗✨ Introducing the all-new AC Cobra GT Coupe! 🌟
For the first time in history, this iconic sports car is getting a hardtop version, and it's set to be produced in series! 🏁💨
With stunning carbon body panels, the Coupe is not only beautiful but also powerful! 💪🔥 Choose between two roaring V8 engines: 565 or 730 horsepower!
Starting at just £325,000, this masterpiece is ready to hit the roads! 💷🚀
#ACCobra#SportsCar#CarEnthusiast#LuxuryAutomobile#V8Power#CobraGT#CarLovers#IconicDesign#AutomotiveExcellence
🚗💨Introducing the Doughboy: A Mind-Blowing Restomod with 2838 HP!💥
At the SEMA Show, we unveiled a beast that redefines power and performance! 🔥 This incredible V8 engine boasts a staggering 8.8 liters of displacement and is equipped with dual turbochargers, pushing the limits of automotive engineering. 💪✨
🔧 With the engine strategically shifted 40 cm back towards the center of the wheelbase, every detail has been meticulously crafted from scratch, including the intake, exhaust, and cooling systems. 🛠️
💎 Custom parts were expertly created using CNC machining and 3D printing technology, showcasing innovation at its finest.
Get ready to experience the future of restomods! 🚀
#Doughboy#SEMA2023#Restomod#V8Power#Turbocharged#CarEnthusiast#AutomotiveInnovation#CustomCars#CNC#Auto