Reading/writing files in Python
Question: I am trying to create a new text file of stock symbols in the russell 2k from
one that looks like this:

[![enter image description
here](http://i.stack.imgur.com/eM2nr.png)](http://i.stack.imgur.com/eM2nr.png)

All I want is the ticker symbol at the end of each line. So I have the
following code:

    
    
    with open("russ.txt", "r") as f:
        for line in f:
            line = line.split()
            line = line[-1]
            if line == "Ticker": continue
            print line
            with open("output.txt", "w") as fh:
                fh.seek(0,2)
                print line
                fh.write(line)
    

All I end up with in the `output.txt` file is one line with the very last
ticker in the list instead of all the tickers. I thought using `fh.seek(0,2)`
would create a new line at the end each time through. What am I doing wrong?
Also, in reality I don't need to create another doc, I could just edit the
current one but I couldn't figure that out either so if you could show me how
to just write to the same file that also is perfectly acceptable.


Answer: I believe using
[`fileinput`](https://docs.python.org/2.7/library/fileinput.html?highlight=fileinput#fileinput.input)
will be also handy in your case:

    
    
    import fileinput
    import sys
    
    
    for line in fileinput.input("russ.txt", inplace=1):
        sys.stdout.write(line.split(' ')[-1])
    

`fileinput.input` will change original file.

