read from stdin python
Question: I am trying to take a filename input from the first command line argument and
if there is none then to read from `stdin` but for some reason my `if`
statement does not seem to be working. I have pasted it below. Any help would
be greatly appreciated.

    
    
    import sys
    
    
    filename=sys.argv[1]
    
    if filename==None:
       filename=sys.stdin.readlines()
    


Answer: You should check the length of `sys.argv` before trying to index it:

    
    
    import sys
    if len(sys.argv) > 1:
      filename=sys.argv[1]
    else:
      filename=sys.stdin.readline().strip()
    

The first element in `sys.argv` is always the filename of the python script,
hence the `> 1`.

**[EDIT]**

To expand on fixxxer's answer regarding `is` vs `==`:

`is` returns `True` if the variables compared point to the same object. This
holds true for `None`, as `None` is a singleton. `==` on the other hand,
returns `True` if the variables compared are equal, i.e. they can point to two
distinct (but equal) objects and `==` will yield `True`.

