title: Python: reading filename from command line argument
body: <p>I am making a simple script that counts some values from a csv file generated from google forms.</p>
  
  <p>The script is as follows:</p>
  
  <pre><code>import csv
  import os.path
  
  fileName=None
  
  if __name__=="__main__":
      try:
          fileName=argv[1]
          isFile(fileName)
          pass
      except Exception as e:
          print("You must provide a valid filename as parameter")
          raise
  
  def isFile(fileName):
      if(not os.path.isfile(fileName)):
          raise ValueError("You must provide a valid filename as parameter")
  
  print fileName
  
  
  def readCsvAndCountPercentPerFormItemFromGoogleForms(fileName):
      times={}
      totalRows=0
      with open(fileName,'r') as csvfile:
  
          csvReader=csv.reader(csvfile);
  
          for row in csvreader:
  
              value=row[1]
  
              if(value in times.values()):
                  times[value]+=1
              else:
                  times[value]=1
  
              totalRows+=1
  
          return calculateDictionaryAsPercent(times,totalRows)
  
  def calculateDictionaryAsPercent(times,totalRows):
  
      if(totalRows==0):
          raise ValueError("The file does not contain any rows")
  
      for key,val in times.items():
              times[key]=(val/totalRows)*100
  
      return times
  
  
  finalTimes=readCsvAndCountPercentPerFormItemFromGoogleForms(fileName)
  
  print finalTimes
  </code></pre>
  
  <p>But I get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "csv.py", line 1, in &lt;module&gt;
      import csv
    File "/home/pcmagas/Kwdikas/python/csv.py", line 54, in &lt;module&gt;
      finalTimes=readCsvAndCountPercentPerFormItemFromGoogleForms(fileName)
    File "/home/pcmagas/Kwdikas/python/csv.py", line 26, in readCsvAndCountPercentPerFormItemFromGoogleForms
      with open(fileName,'r') as csvfile:
  TypeError: coercing to Unicode: need string or buffer, NoneType found
  </code></pre>
  
  <p>The problem is that for some reason the variable <code>fileName</code> does not change value in:</p>
  
  <pre><code>if __name__=="__main__":
      try:
          fileName=argv[1]
          isFile(fileName)
          pass
      except Exception as e:
          print("You must provide a valid filename as parameter")
          raise
  </code></pre>
  
  <p>The following piece of code was base ton the first answer found on <a href="https://stackoverflow.com/questions/14016742/detect-and-print-if-no-command-line-argument-is-provided">Detect and print if no command line argument is provided</a></p>
  
  <p>So can you offer me a solution because I do not write python too often, therefore I did the following script in order to learn it better.</p>
  
  <h1>Edit 1</h1>
  
  <p>The code has been reformated to this:</p>
  
  <pre><code>import csv
  from sys import argv
  import os.path
  
  
  def isFile(fileName):
      if(not os.path.isfile(fileName)):
          raise ValueError("You must provide a valid filename as parameter")
  
  
  def readCsvAndCountPercentPerFormItemFromGoogleForms(fileName):
      times={}
      totalRows=0
      with open(fileName,'r') as csvfile:
  
          csvReader=csv.reader(csvfile);
  
          for row in csvreader:
  
              value=row[1]
  
              if(value in times.values()):
                  times[value]+=1
              else:
                  times[value]=1
  
              totalRows+=1
  
          return calculateDictionaryAsPercent(times,totalRows)
  
  def calculateDictionaryAsPercent(times,totalRows):
  
      if(totalRows==0):
          raise ValueError("The file does not contain any rows")
  
      for key,val in times.items():
              times[key]=(val/totalRows)*100
  
      return times
  
  
  fileName=None
  
  if __name__=="__main__":
      try:
          fileName=argv[1]
          print("Filename: ",fileName)
          isFile(fileName)
          pass
      except Exception as e:
          print("You must provide a valid filename as parameter")
          raise
  
  print("Filename: ",fileName)
  finalTimes=readCsvAndCountPercentPerFormItemFromGoogleForms(fileName)
  
  print finalTimes
  </code></pre>
  
  <p>Still the same error.</p>