title: MapReduce in python: os.environ ["map_input_file"] dosen't work in map.py
body: <p>This is my first time to study Hadoop MapReduce with python.</p>
  
  <p>I wrote a map.py to get filenames of two files in order to learn how to join two files.
  Here are two CSV files:</p>
  
  <p><em>worksheet1.csv</em></p>
  
  <pre><code>sno,name
  1,name1
  2,name2
  3,name3
  4,name4
  </code></pre>
  
  <p><em>worksheet2.csv</em></p>
  
  <pre><code>sno,courseno,grade
  1,1,80
  1,2,90
  2,1,82
  2,2,95
  </code></pre>
  
  <p>map.py:</p>
  
  <pre><code>#!/bin/bash
  # -*- coding: utf-8 -*-
  import os
  import sys
  
  def mapper():
  
      filepath = os.environ["map_input_file"] 
      filename = os.path.split(filepath)[-1]  #get the names
      for line in sys.stdin:
          if line.strip()=="":
              continue
          fields = line[:-1].split("\t") 
          sno = fields[0]   #get student ID
  
          if filename == 'worksheet1':
              #get student ID and name, mark 0
              name = fields[1] 
              print '\t'.join((sno,'0',name)) 
          elif filename == 'worksheet2':
              #get student ID, course number, grade, mark 1
              courseno = fields[1]
              grade = fields[2]
              print '\t'.join((sno,'1',courseno,grade)) 
  
  
  if __name__=='__main__':
      mapper()
  </code></pre>
  
  <p>Then I use</p>
  
  <pre><code>    $cat worksheet1 worksheet2 |python map.py
  </code></pre>
  
  <p>to test the program.</p>
  
  <p>The error shows below:</p>
  
  <pre><code>Traceback (most recent call last):
    File "map.py", line 30, in &lt;module&gt;
      mapper()
    File "map.py", line 11, in mapper
      filepath = os.environ['map_input_file']
    File "/usr/lib64/python2.7/UserDict.py", line 23, in __getitem__
      raise KeyError(key)
  KeyError: 'map_input_file'
  </code></pre>
  
  <p>Please tell me why and how to modify the code.
  Thank you very much!</p>