DEV Community

Cover image for Automating Simple Things with Python is Awesome

Automating Simple Things with Python is Awesome

Ryan Palo on November 03, 2018

Cover photo by Tim Easley on Unsplash. This is just a quick post about something that happened to me yesterday at work. So there I was: working h...
Collapse
 
liana profile image
Liana Felt (she/her)

(My inner startup is well-staffed.)

I hope you never face layoffs 😱🀞

Collapse
 
rpalo profile image
Ryan Palo

We’ve got a pretty stable business model, as long as I keep bringing in new knowledge and practice. πŸ˜ƒ

Collapse
 
rrampage profile image
Raunak Ramakrishnan • Edited

Here's one way of doing this using the standard Linux utils

source <(find . -type f -name "*.1" | sed -nr "s/(^.*)\.(.*)\.1/mv \1.\2.1 \1.\2/p")

Explanation:

  • source <(CMD) executes text output of CMD as a command.
  • find . -type f -name "*.1" finds all files in current directory ending with .1
  • sed -nr "s/(^.*)\.(.*)\.1/mv \1.\2.1 \1.\2/p" uses a regex to get the basename and extension before .1 and outputs a move command with .1 removed.
  • The output of this pipeline is executed by source

We can also use exec option from find itself but I didn't go down that path :)

Collapse
 
rpalo profile image
Ryan Palo

Neat! Thanks for sharing πŸ˜„ you could also do it in pure Bash with a for loop and string indexing. But it’s not as nifty as your way.

Collapse
 
rrampage profile image
Raunak Ramakrishnan • Edited

Yes. Another way I found was using awk instead of source as follows:

find . -type f -name "*.1" | sed -nr "s/(^.*)\.(.*)\.1/mv \1.\2.1 \1.\2/p" | awk '{system($0)}'

This makes the eval of strings part of the pipeline.

Collapse
 
alialhajji profile image
Ali Alhajji

I once faced a similar issue. My boss, who isn't an IT guy at all, wanted to change the extension of many files. He told me to do it (for I was the new hire then). All the files had a common extension. I did not have to write a code to automate it, I just opened cmd and used the command (rename *.zip *.xlsx)

that's it!

As I was reading through your post, this solution kept popping up in my mind. But then I realized that you had different extensions to change.

Collapse
 
rpalo profile image
Ryan Palo

Yeah, it would have been a lot easier if I hadn't needed to preserve some amount of the original extension. But definitely a PowerShell one-liner was my first thought.

Collapse
 
mark_nicol profile image
Mark Nicol

Python is such an awesome glue/hack language. I love your storytelling ability. And thanks for introducing me to glob. That's such a useful command to know for the future.

Collapse
 
rpalo profile image
Ryan Palo

Thanks! Glad you liked it!

Collapse
 
rhymes profile image
rhymes

Ahah nice story 🀣

Collapse
 
wajahatkarim profile image
Wajahat Karim πŸ‡΅πŸ‡°

If it wasn't for your inner developer, this would have been the most boring and useless article ever.
But your inner developer made it way interesting and amazing to read. Hi five to that inner developer.
PS. I am going to put my next article this way. Wish me luck from your inner developer :)

Collapse
 
rpalo profile image
Ryan Palo

Um. Thank you, I think?

Collapse
 
pauljacobson profile image
Paul Jacobson

Thanks for sharing this. I love these little scripts that do awesome, little tasks so well.

Collapse
 
rpalo profile image
Ryan Palo

Glad you liked it!

Collapse
 
pavonz profile image
Andrea Pavoni

Syntax is important for expressiveness. I love both Python and Ruby for their own strengths. In this case && IMHO, Ruby wins with its one-line that mostly reads like plain english.