So I've done a lot of browsing, but I cant seem to open my text file so I can begin to manipulate it.
I saved my program in with the same file: and my first line returns no errors:
with open(fpath) as f_in:
But now is my file open? The text file (file_list.txt) is:
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.
ps. If anyone knows how to change my hard drives name to something other than veggiepunk1363 PLEASE let me know! haha
my new code:
f = open('/Users/veggiepunk1363/Desktop/geoproccessing/file_listmaster3.txt','r')
filedata = f.read()
Jan = filedata.replace("20001","2000Jan")
Feb = filedata.replace("20002","2000Feb")
f = open('/Users/veggiepunk1363/Desktop/geoproccessing/file_listmaster3.txt','w')
print ('The dates have been reformatted')
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.
You need not use a context manager. This is much simpler and does not require you to explicitly close the file descriptor:
for file_name in open(fpath).readlines(): print file_name # 3B43.20000101.7.tif...
Try this way.
veggiepunk1363 is not the name of your hard drive. It is the folder in which your
User files resides on Windows.
fpath= r'/Users/veggiepunk1363/Desktop/geoproccessing/file_list.txt' with open(fpath, 'r') as f_in: #opens the file with read mode dates = f_in.readlines() #returns a list of all the lines in the file into dates