opening and manipulating text files with python
<p>So I've done a lot of browsing, but I cant seem to open my text file so I can begin to manipulate it. </p>

<p>I saved my program in with the same file: and my first line returns no errors:</p>

<pre><code>fpath= r'/Users/veggiepunk1363/Desktop/geoproccessing/file_list.txt'

with open(fpath) as f_in:
    dates= f_in.read()
    f_in.close()
</code></pre>

<p>But now is my file open? The text file (file_list.txt) is: </p>

<p>3B43.20000101.7.tif</p>

<p>3B43.20000201.7.tif</p>

<p>3B43.20000301.7.tif</p>

<p>3B43.20000401.7.tif</p>

<p>3B43.20000501.7.tif...</p>

<p>and so on, so now I need to assign dates(jan, feb, mar etc) instead of numbers, but I havent used anything without "" so I'm a little lost and while I can find a lot on CSV files I havent been able to find what I need on txt files so any references would be much appreciated. </p>

<p>Thanks! </p>

<p>ps. If anyone knows how to change my hard drives name to something other than veggiepunk1363 PLEASE let me know! haha</p>

<p>my new code:</p>

<p>f = open('/Users/veggiepunk1363/Desktop/geoproccessing/file_listmaster3.txt','r')
filedata = f.read()
f.close()</p>

<p>Jan = filedata.replace("20001","2000Jan")</p>

<p>Feb = filedata.replace("20002","2000Feb")</p>

<p>Mar= filedata.replace("20003","2000Mar")</p>

<p>Apr= filedata.replace("20004","2000Apr")</p>

<p>May= filedata.replace("20005","2000May")</p>

<p>Jun= filedata.replace("20006","2000Jun")</p>

<p>Jul= filedata.replace("20007","2000Jul")</p>

<p>Aug= filedata.replace("20008","2000Aug")</p>

<p>Sep= filedata.replace("20009","2000Sep")</p>

<p>Oct= filedata.replace("20010","2000Oct")</p>

<p>Nov= filedata.replace("20011","2000Nov")</p>

<p>Dec= filedata.replace("20012","2000Dec")</p>

<p>f = open('/Users/veggiepunk1363/Desktop/geoproccessing/file_listmaster3.txt','w')</p>

<p>f.write(Jan)</p>

<p>f.write(Feb)</p>

<p>f.write(Mar)</p>

<p>f.write(Apr)</p>

<p>f.write(May)</p>

<p>f.write(Jun)</p>

<p>f.write(Jul)</p>

<p>f.write(Aug)</p>

<p>f.write(Sep)</p>

<p>f.write(Oct)</p>

<p>f.write(Nov)</p>

<p>f.write(Dec)</p>

<p>print ('The dates have been reformatted') </p>

<p>f.close()</p>

<p>I know this is the long way to do what I'm trying to do, but I thought it would still work... it didnt. it tripled the size of my txt file and it looks like everything was duplicated? not quite sure what is going on. </p>

<p>You need not use a context manager. This is much simpler and does not require you to explicitly close the file descriptor:</p>

<pre><code>for file_name in open(fpath).readlines():
    print file_name  # 3B43.20000101.7.tif...
</code></pre>

python file text