0

Edited my program - still having same issue

Also, the linked answer that was recommened is useless as it only tells you that you cannot modify a file in place and does not offer any good solution. I have a file that has line numbers at the start of it. I wrote a python script to eliminate these line numbers. This is my second attempt at it and I am still having the same issues

First I open the file and save it to a variable to reuse later:

#Open for reading and save the file information to text
fin = open('test.txt','r')
text = fin.read()
fin.close 

#Make modifications and write to new file
fout = open('test_new.txt','w') 
for line in text: 
    whitespaceloc = line.find(' ') 
    newline = line[whitespaceloc:] 
    fout.write(newline) 

fout.close()

I have also tried using the 'with' keyword with no luck, When I open test_new.txt it is empty

What is going on here?

Community
  • 1
  • 1
user2202911
  • 2,898
  • 5
  • 20
  • 28
  • 4
    You don't need `file.close()` when you open it in a `with` block. Also, `"%s" %newline` could be replaced with only `newline`. Have you tried other opening flags, like `w`? – Aleksander Lidtke Sep 09 '13 at 15:42
  • 4
    See http://stackoverflow.com/questions/5453267/is-it-possible-to-modify-lines-in-a-file-in-place for modifying files in-place. – Nicu Stiurca Sep 09 '13 at 15:42
  • @AleksanderLidtke thanks for the tips.. I just changed it to w+ and all of my text got deleted, at least something happened. I am surprised that there is a difference though - according to the documentation I read both r+ and w+ open the file for reading and writing – user2202911 Sep 09 '13 at 15:51
  • As @SchighSchagh suggested if you just avoid modifying the file *in place* it will work just fine. – Aleksander Lidtke Sep 09 '13 at 15:56
  • Is your line numbering something like 1., 2., 3. ? or just 1 2 3? – Games Brainiac Sep 09 '13 at 16:06
  • @GamesBrainiac the latter – user2202911 Sep 09 '13 at 16:09
  • @SchighSchagh That link is useless. All I learned is that modifying files in-place cannot be done. There is no good solution being offered. – user2202911 Sep 09 '13 at 20:08
  • @user2202911 you didn't seem to read the accepted answer very carefully. It shows exactly how to modify files in place using the `fileinput` module. – Nicu Stiurca Sep 12 '13 at 22:55

1 Answers1

3

My advice on how to do this would be:

1) Read the file to a buffer:

 with open('file.txt','r') as myfile:
      lines=myfile.readlines()

2) Now close and overwrite the same file with any changes you want to do just as you did before:

 with open('file.txt','w') as myfile:
      for line in lines: 
         whitespaceloc = line.find(' ') 
         newline = line[whitespaceloc:] 
         myfile.write("%s" %newline) 
Aleksander Lidtke
  • 2,876
  • 4
  • 29
  • 41
  • I modified my program based on your recommendations and am still having errors – user2202911 Sep 09 '13 at 20:04
  • I'm sorry. The program is running fine without errors. However, when I open up the text file it is empty – user2202911 Sep 09 '13 at 20:13
  • 2
    @user2202911 you use `fin.read()` not `readlines()`. Which means that you iterate over **EACH INDIVIDUAL CHARACTER IN THE FILE** not the lines as the output from `file.read()` is a string, not a list of lines. You then proceed to find whitespaces in only 1 character-long strings, so the output file is empty. Change your code to what I wrote exactly and it will work. – Aleksander Lidtke Sep 09 '13 at 20:17
  • It worked.. there is no mention of that in the documentation, hence the confusion – user2202911 Sep 09 '13 at 20:28