-4

I have some txt files in '\data' such as '1.txt'and '2.txt'. Now I want to add the last line of these two files in the another file named '3.txt' but I'm only able to add the last line of '1.txt' into the '3.txt'

from sys import argv

from os.path import exists

script,from_file,to_file=argv

in_file=open(from_file) in_data=in_file.readlines() count=len(in_data)

#print in_data[3] print count line=in_data[count-1]

in_file.close()

out_file=open(to_file,'w')
out_file.write(line)

out_file.close()
in_file.close()
zyMacro
  • 675
  • 7
  • 14

2 Answers2

0

You are opening the output file with 'w', which means 'write', so it will override any existing content (as opposed to 'a' - append).
So, assuming you run the script multiple times on each file in the \data folder, you are just overriding the file over and over again, that's why you get only a single line at the end.

yossiz74
  • 889
  • 1
  • 9
  • 16
  • Thank you! That's where I confused. But how can I add something in a new line rather than in the behind of the last line? – zyMacro Apr 06 '16 at 14:19
  • You can probably settle for out_file.write('\r' + line) – yossiz74 Apr 06 '16 at 14:25
  • Thank you for your help. The last question:Can you tell me how to add the last line of all txt files under a dir into a new text?As you know,It‘s a waste of time to add a line one by one in command line. – zyMacro Apr 06 '16 at 14:36
  • See http://stackoverflow.com/questions/3964681/find-all-files-in-directory-with-extension-txt-in-python – yossiz74 Apr 06 '16 at 14:46
  • I've deal with that problem.I‘d like to express my gratitude. – zyMacro Apr 07 '16 at 02:21
0

As yozziz74 mentioned you are opening the file in write mode and will therefore override every time but you also haven't defined the line variable you want to write to the file. If the desired effect is to override each then keep the 'w' otherwise change to 'a'.

This code should do what you're looking for:

from os import listdir
from sys import argv
from os.path import exists

files = [ file for file in listdir('.') if file.endswith('.txt') ]

to_file=argv[1]

out_file=open(to_file,'a')

for file in files:
    in_file=open(file)
    lines=in_file.readlines()

    last_line=lines[-1]
    in_file.close()

    out_file.write(last_line)
    in_file.close()

out_file.close()
logileifs
  • 65
  • 1
  • 8
  • Can you tell me how to add the last line of all txt files under a dir into a new text?It‘s a waste of time to add a line one by one in command line. – zyMacro Apr 06 '16 at 14:28
  • I would recommend using the method mentioned [here](http://stackoverflow.com/a/3964691/3326114) to list all the txt files and then combine it with the code I posted above – logileifs Apr 06 '16 at 14:48
  • I have updated my answer so the code also lists all files in the current directory, after that it's simply a matter of looping through them all and read the last line from them – logileifs Apr 06 '16 at 14:56
  • @zyMacro please look at my edited answer and see if that does the job for you – logileifs Apr 06 '16 at 15:07
  • Thank you!I have received help from yossiz74.And what you said is same with his answer. – zyMacro Apr 07 '16 at 02:25