Warehouse Inventory With Python

Reading time ~2 minutes

Hi all,

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:

m464.pwd
{\pwd2\ansi{\*\pwdcomment
************************************************************************
*
* This is a Microsoft WordPad document.
*
* For further details visit the Microsoft Windows CE web site, at
*         http://www.microsoft.com/windowsce
* Or search MSDN for 'Microsoft WordPad'
*
************************************************************************
}\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Tahoma;}}
{\colortbl ;}
\viewkind4\uc1\pard\cf0\f0\fs22 008022376\par
008022375\par
008022379\par
008022380\par
008022381\par
\par
}
^@

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:

Inventory Count
Generated: 03/09/2010 21:45:37
------------------------------------------------
Filename           # of Cartons
------------------------------------------------
m468                602
m466                286
m461                144
m465                245
m464                199

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.

#Spencer Smith
#Inventory Calculator
#Created 3/9/2010
 
import sys, os
from time import strftime
 
#Name of output file
filename2 = "inventory_count.txt"
 
#Get all files in current directory
total_files = os.listdir(os.getcwd())
pwd_files = list()
 
#Find all files with '.pwd' extension
for x in total_files:
   if x.find(".pwd") != -1:
     pwd_files.append(x)
 
#Return error to 'error.log' if there are no '.pwd' files in the directory
if len(pwd_files) == 0:
  FILE = open("error.log","w")
  FILE.write(strftime("%m/%d/%Y %H:%M:%S") + " -- No files '.pwd' files in directory to read.")
  FILE.close()
  sys.exit(-1)
 
#Open output file for writing
OUT = open(filename2,"w")
 
#Create header
OUT.write("Inventory Count\n")
OUT.write("Generated: "+strftime("%m/%d/%Y %H:%M:%S")+"\n")
OUT.write("------------------------------------------------\n")
OUT.write("Filename           # of Cartons\n")
OUT.write("------------------------------------------------\n")
 
#Iterate through available '.pwd' files
for name in pwd_files:
 
#Open file for reading
   FILE = open(name,"r")
   counter = 0
   suffix = "\par"
 
#Search each line for "\par". "\par" on the line means that there is a carton on this line.
#Increment carton counter if found
   for line in FILE:
      if line.find(suffix) != -1:
        counter = counter + 1
#After reading all lines in file, write filename and count to output file.
   OUT.write("%s                %d\n" % (name[0:-4],counter-1))
#Close current file and reset.
   FILE.close()
 
#Close output file so that it can be read.
OUT.close()

Enjoy! Hope someone finds it useful!

KubeDNS Tweaks for Performance

Hey y'all. Wanted to document some of the stranger bits I've encountered while running Kubernetes with one of my clients. We've finally g...… Continue reading

Reporting to CloudWatch from CoreOS

Published on March 04, 2017