user
user

Reputation: 97

How can i avoid new line at the end of the file?

i need to use

for line in doc.split('\n'):

and do some operation on each line but i got at the end of the file empty line as i think it split a new line every time ! how can i avoid this problem ?

Upvotes: 0

Views: 35

Answers (1)

Riccardo Bassani
Riccardo Bassani

Reputation: 51

Please rephrase your question, since it is not very clear.

Anyway, if you are working with a text file you can just use:

with open("path_to_soruce_textfile", "r") as src, open("path_to_dest_textfile", "w") as dst:
    for line in src.readlines():            # this gives you a list of lines with each line finishing with \n
       processed_line = modidy(line)
       dst.write(processed_line)            # make sure \n is at the end of each line when you write

# the file is automatically closed

Upvotes: 1

Related Questions