So this week, I’ve been in a crunch to get some last minute cash for spring break. My Dad had a little opportunity for me to write a Python script for him to use at the office. The back story is basically that the warehouse where he works has to take inventory using some sort of hand-held scanning devices.
He wanted me to write a script that, when placed in a directory with any given number of output files from the devices, generates a document stating each file and how many cartons are in each pallet. Sounds like a perfect job for Python, right? Exactly right. It literally took me about 45 minutes to 1 hour and I had the thing completely off the ground running. I’ve decided to post it not so much because this exact program is really useful to anyone else, but because I definitely learned some cool I/O stuff while doing it.
First, I think it’s useful to get an idea of how the files looked coming in. They were basically Wordpad documents generated by Windows CE and didn’t have much format to them. Here’s a little example:
As you can see, It’s a little hard to decipher. Also, keep in mind that this is not a full file as I didn’t want to post 600 lines in there. Anywhere there is a ‘\par’, except for the lone one at the end, there is a carton in the warehouse. All I had to do was figure out a way to add.
My program gave the following output:
So, as you can see, it seems to work pretty well. I can also imagine this is WAY easier than counting by hand. So, here is the code that converts one to the other. Be sure to notice the usefulness of the calls like getcwd(), which proved to be awesome for finding all the files in the directory.