I'm a noob, there got that outta the way.

Wrote this to serve as a very basic inventory control for our store's iPad inventory. Lets junior employees count our inventory, and then appends the count in a text file.

99% there for what I want, but having trouble getting it to write a particular bit to the file:

import operator
import datetime
#time
timestamp = str(datetime.datetime.now())

i16w = input("iPad 16GB WiFi?:")
i32w = input("iPad 32GB WiFi?:")
i64w = input("iPad 64GB WiFi?:")
i16g = input("iPad 16GB 3G?:")
i32g = input("iPad 32GB 3G?:")
i64g = input("iPad 64GB 3G?:")

# create an inventory list of tuples
# each tuple contains the item name and the count of the item
inventory = [('16GB wifi', i16w),('32GB wifi', i32w),('64GB wifi', i64w),('16GB 3g', i16g),('32GB 3g', i32g),('64GB 3g', i64g)]

print '-'*50  # 50 dashes, cosmetic stuff

# establish the sort key
# each tuple is zero based
# item name = 0 and item count = 1
getcount = operator.itemgetter(1)

print "Inventory list sorted by item count:\n"
# now sort by item count
sortedInventory = sorted(inventory, key=getcount)

# more official look. For staff to see their result.
print '\n'*150
print '/'*50
print "iPad inventory:\n"+timestamp
print '/'*50

for item in sortedInventory:
    print "%-10s%6d" % (item[0], item[1])

That all works, but below is where my trouble starts:

#write & append to file.
ipadcount = "/Users/sin/ipadcount.txt"
file = open(ipadcount, 'a')
file.write ('/'*50)
file.write ("\niPad inventory:")
file.write ("\n")
file.write (timestamp)
file.write ("\n")
file.write ('/'*50)
#want write the output below to the file, but dunno how!
for item in sortedInventory:
	file.write "%-10s%6d" % (item[0], item[1]) #bombs here, not sure why
file.close()

It bombs out on that line, tried a number of things, but not sure why it won't write the "%-10s%6d" part.

Recommended Answers

All 2 Replies

file.write ( "%-10s%6d" % (item[0], item[1]) )
           ^                                 ^

Worked fine for me after I put these parentheses in.

That it does! Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.