python - Keyerror in reading csv file -


i trying write script pass file name argument shell script python script , python script processes script.
it giving me keyerror if run same script hardcoding file name works fine.

#!/bin/sh lockfile=./test.txt if [ -e ${lockfile} ] && kill -0 `cat ${lockfile}`;   echo "already running"   exit fi  trap "rm -f ${lockfile}; exit" int term exit echo $$ > ${lockfile}  # stuff  files=/home/sugoi/script/csv/*  file in $files    python ./csvtest.py $file   #mv $file ./archive   done   rm -f ${lockfile}  exit 

python:

from pymongo import mongoclient import csv import json import sys  client = mongoclient() db = client.test  arg in sys.argv:     try:     csvfile = open(arg, 'r')#if hardcode file name here works fine   except ioerror e:       #write error log        sys.exit(100)    reader = csv.dictreader(csvfile)   header=reader.next()     each in reader:       row={}       field in header:            row[field]=each[field]     db.test.update({"_id": row["customerid"]}, {"$push": {"activities":{"action": row["action"],"date" :row["timestamp"],"productid":row["productid"]}}},true) 

what doing wrong ?

two issues.

  1. your shell script isn't expanding file list correctly.

files=/home/sugoi/script/csv/* needs like:

files=`ls -1 /home/sugoi/script/csv/*;` 
  1. your argument python script 1 file @ time, why loop through sys.argv?

just use argument itself, sys.argv[1]. @brian besmanoff pointed out, needs indexed 1 because script name stored in sys.argv[0].

try:     csvfile = open(sys.argv[1], 'r') except ioerror e:     (...) 

finally: can parse directories python instead of looping in shell script. @ os module, particularly os.listdir(). little more work , can have whole thing running inside 1 python script instead of juggling between shell , calling script.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -