Category Archives: Python

Python Snippet: Search for Files Recursively

Currently I’m working on a script to search for duplicate images on my hard drive. To do so, I need to search recursively and go down through all subdirectories.

Recursive searching can be done by importing the commonly used os module. Use the os.walk() function to recursively enumerate file names and paths in the supplied directory. There are then many ways to identify file types, but in this case I’m using fnmatch. Here’s the official fnmatch description:

This module provides support for Unix shell-style wildcards, which are not the same as regular expressions (which are documented in the re module).

We have the modules to use, now the code:

This function, called find_images(), returns an array of full file paths within a given directory that have a file type of ‘.jpg’. While searching, once an image is found we join the image name with root, the current directory being searched by os.walk(). This gives us back a list of full paths instead of just file names.

Example usage:

Hopefully I’ve explained the code clearly, but if not let me know. If you have any questions at all please feel free to ask in the comments.

Whenever you’re reading this, I hope you’re having a great day!

Basic Python Threading

The Python core module Threading provides a really simple way to add concurrency to any program. Concurrency is when a computer executes several instructions simultaneously. If your code waits for a function, but there are other things it could or should be doing in the meantime, try using threading. If threads aren’t quite enough and you feel the need to leverage multiple processors, use the multiprocessing module instead. It is more powerful but also has more overhead to load up.

There are many times where threading is useful, but none so common as a GUI-based program. Python is run sequentially so any little delay will hold up the user interface until the interpreter is done running the other code. For example, a label needs to update to show progress while files are downloaded. If a GUI button calls a function, the whole function has to finish before the interface can regain control and update the window.

Here’s a little example of a timer function that counts up to the specified number of seconds. This is perfect for threading in a GUI so a label can be updated with the current time. After defining the function, a thread is initialized and started up.

Note how quotes are needed around ‘timer_length’ in the keyword args hash in the Thread constructor, but not in the original function definition.

I haven’t tried any advanced threading setups yet, only basics like downloading emails with imaplib and running a timer for an indicator app. Let me know in the comments if you have any questions or if you thought of any cool uses of threading in your own code!

Links: Have Fun Learning Python

This week has been a lot about making jewelry and a little about coding. Even still, I’ve found some great links for learning Python that I’d like to share.

  • Robot Game – This is a really fun way to practice Python once you have the basics down. You use the Robot Game API to write a robot script that competes with other coders’ robots! Upload as many robot ‘AI’ scripts as you’d like and they’ll fight it out in automated battles.
    • “Robot Game is a game for hackers. You write robots in Python, test them with our kit, and submit them to our server. You can challenge other robots yourself, or wait for the server to match you up automatically.”
  • Python Guide to Structuring Your Project – This guide really helped me understand the basics of project layout in Python. I found it by searching for the meaning and use of the file I see so often.  What I discovered was a slew of other helpful tips about variable naming, object types, and importing modules. If you’re new to Python and looking to start your own project, this is an excellent guide to get you started.
  • Popular Python Recipes – This is a great site for seeing how other people solve small problems. A lot of people give the advice of looking at the code for modules you use to basically see how experienced coders get it done. However, I find it’s often just as helpful to see an assortment of smaller solutions. Perhaps one of them will even give you an idea for your next project!
  • – Usually I don’t enjoy watching videos all that much as I generally prefer reading. I can set my pace and reread or stop on something to explore it further. However, sometimes I’m doing something that takes a bit more concentration (like making chainmaille!) and it’s great to throw on some content I can learn from. This collection is almost entirely videos from Python Conferences. The talks are often quite entertaining and on a broad variety of subjects. So if you’re not looking for a video course and instead just something to view for extra Python-to-brain exposure, check out this collection.
  • CoderByte – This site offers three difficulties of programming challenges to complete. It’s actually not just Python either. They offer challenges in Javascript, Python, PHP, Ruby, Java, C, and C++. The challenge is all done in the browser and is meant to test your ability to work with the different fundamentals of the language you choose. Easy and Medium challenges are timed and give bonus points for completing faster. Hard problems are not timed. Here I’ll list an example challenge description from each section:
    • [Easy] Palindrome – Determine if a string is written the same way forward and backwards.
    • [Medium] Array Addition – Determine if numbers in an array can add up to a certain  number in the array.
    • [Hard] Noughts Determiner – Determine the winning position of a Tic-tac-toe board.
  • Dungeons and Developers – This one isn’t specifically Python at all, but it’s a really fun concept that lays out the progression needed to become a web developer. The site displays a skill tree that looks like it could have come from any classic RPG game. Each part of the tree is relating to a different aspect of web development (ie Backend, Frontend, Design, Deployment). For each skill, there are links to tutorials that will help you learn the required knowledge to say “I know this stuff, I have this skill now.” It’s just a really fun and interactive way to challenge yourself and push toward a tangible learning goal.

Let me know if you use any of these and what you think of them. I’d also love to know about any similar resources I might like. I hope these can help you in your code-learning adventures!

Python Snippet: Get Terminal Width

There is a wonderful command on UNIX systems to return the width of the current terminal. Enter  tput cols and see for yourself! Tput can actually provide a lot of different info by querying the terminfo database. Really all I’ve needed though is ‘cols’ for columns.

I was writing a Python CLI app, and wanted to format and print output in a nice table. To do that (without downloading a table printing module) I had to know how much space was available for printing. Can’t make a nice-looking set of data if every other line is being wrapped!

Here is a simple function that returns the width of whatever terminal is running the script. Subprocess is the most current built-in Python module for running system commands. It contains a selection of different functions each with many optional parameters, depending on the complexity of your needs. Using subprocess.check_output() is the way to go if you are looking to get back the output of a command. The  check_output() function has a whole slew of accepted arguments, but in this simple instance we need only one: a list containing command to run and the arguments to pass to it.

The first exception will be raised if the system returns an error when trying to execute the tput command. The second exception will be raised if the command (in our case tput) has a problem with the given argument ( cols). If nothing goes wrong, the output will be a nice whole number that you can use to determine exactly how many characters can be printed per line!

Have you ever needed to know the width of your terminal? If you have any questions please feel free to ask in the comments below!

Here’s a link to this code snippet on Github Gists:

From Ruby to Python

For a while I’ve been thinking about switching to Python, especially when I see so many programs I like to use written in it. My favorite terminal Guake comes to mind first and of course my all-time favorite editor Sublime Text. The community around Ruby is so heavily focused on web development that other areas seem to be neglected. When I first started getting into coding, web seemed the way to go. I got caught up in the hype and forgot what my gut told me. I love CLI apps and sysadmin tasks. I’ve also found I really enjoy GUI toolkits.  As a language Ruby is really wonderful, but the Python ecosystem just seems like a better fit for me. In January I decided to take the plunge. I came down with something and was confined to bed for a week so it was the perfect time to spend learning. I’m so happy with the change already. I’m writing more and better code in Python than I ever did in Ruby. First step was a beginner’s tutorial. I started with the free Google Python class. I usually prefer purely reading material, but the videos were really enjoyable. Made me feel like I was back in a classroom environment again.

Python is amazing me every day with how many modules and packages are available. After doing a bit of scripting, I started picking up the Kivy framework. It’s an “Open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps.” Python makes me feel empowered to create any coding idea I can dream up. If you’re thinking of learning to code or want to try a new language, I strongly suggest checking out the list of tutorials on the Python wiki. Python is amazingly fun to write and so clean to read. White space as syntax makes for such simple and readable code. I’ve been using it barely over three weeks and already I want to rewrite all of my utility scripts from Ruby to Python.

Let me know in the comments if you have any questions about starting out in either Python or Ruby! And for those interested I have a large list of links to tutorials and useful learning materials on my Pinboard.