105

I can do this using a separate file, but how do I append a line to the beginning of a file?

f=open('log.txt','a')
f.seek(0) #get to the first position
f.write("text")
f.close()

This starts writing from the end of the file since the file is opened in append mode.

2

12 Answers 12

145

In modes 'a' or 'a+', any writing is done at the end of the file, even if at the current moment when the write() function is triggered the file's pointer is not at the end of the file: the pointer is moved to the end of file before any writing. You can do what you want in two manners.

1st way, can be used if there are no issues to load the file into memory:

def line_prepender(filename, line):
    with open(filename, 'r+') as f:
        content = f.read()
        f.seek(0, 0)
        f.write(line.rstrip('\r\n') + '\n' + content)

2nd way:

def line_pre_adder(filename, line_to_prepend):
    f = fileinput.input(filename, inplace=1)
    for xline in f:
        if f.isfirstline():
            print line_to_prepend.rstrip('\r\n') + '\n' + xline,
        else:
            print xline,

I don't know how this method works under the hood and if it can be employed on big big file. The argument 1 passed to input is what allows to rewrite a line in place; the following lines must be moved forwards or backwards in order that the inplace operation takes place, but I don't know the mechanism

4
  • 2
    with open(filename,'r+') as f: will close the file? Sep 15, 2014 at 17:52
  • 9
    @TomBrito Yes, the new with statement allows you avoid common try/except/finally boilerplate code. It was added in Python version 2.5.
    – Jesse Webb
    Apr 1, 2015 at 14:12
  • 3
    1st way removes the first line.
    – SanketDG
    Jul 1, 2015 at 1:39
  • 2
    @SanketDG Is that true for the latest answer? I don't see that behavior with the first method.
    – Dave C
    May 16, 2019 at 18:38
19

In all filesystems that I am familiar with, you can't do this in-place. You have to use an auxiliary file (which you can then rename to take the name of the original file).

18

To put code to NPE's answer, I think the most efficient way to do this is:

def insert(originalfile,string):
    with open(originalfile,'r') as f:
        with open('newfile.txt','w') as f2: 
            f2.write(string)
            f2.write(f.read())
    os.remove(originalfile)
    os.rename('newfile.txt',originalfile)
3
  • 5
    Disastrous! Copying all the file into a new one with the line. Jan 13, 2019 at 9:00
  • 23
    @TonyTannous if you've got a way to do it without, please do share. I know a lot of people would be interested.
    – jeffpkamp
    Jan 14, 2019 at 0:39
  • added for Win OS... os.remove(originalfile) Sep 21, 2022 at 9:20
14

Different Idea:

(1) You save the original file as a variable.

(2) You overwrite the original file with new information.

(3) You append the original file in the data below the new information.

Code:

with open(<filename>,'r') as contents:
      save = contents.read()
with open(<filename>,'w') as contents:
      contents.write(< New Information >)
with open(<filename>,'a') as contents:
      contents.write(save)
2
  • ^^^ This is gold ^^^ Oct 6, 2021 at 19:19
  • No need to open the file a second time in a mode, you can just do two writes in the same with open block.
    – Jeronimo
    Oct 15, 2021 at 12:38
11

The clear way to do this is as follows if you do not mind writing the file again

with open("a.txt", 'r+') as fp:
    lines = fp.readlines()     # lines is list of line, each element '...\n'
    lines.insert(0, one_line)  # you can use any index if you know the line index
    fp.seek(0)                 # file pointer locates at the beginning to write the whole file again
    fp.writelines(lines)       # write whole lists again to the same file

Note that this is not in-place replacement. It's writing a file again.

In summary, you read a file and save it to a list and modify the list and write the list again to a new file with the same filename.

5
num = [1, 2, 3] #List containing Integers

with open("ex3.txt", 'r+') as file:
    readcontent = file.read()  # store the read value of exe.txt into 
                                # readcontent 
    file.seek(0, 0) #Takes the cursor to top line
    for i in num:         # writing content of list One by One.
        file.write(str(i) + "\n") #convert int to str since write() deals 
                                   # with str
    file.write(readcontent) #after content of string are written, I return 
                             #back content that were in the file
4

There's no way to do this with any built-in functions, because it would be terribly inefficient. You'd need to shift the existing contents of the file down each time you add a line at the front.

There's a Unix/Linux utility tail which can read from the end of a file. Perhaps you can find that useful in your application.

1

If the file is the too big to use as a list, and you simply want to reverse the file, you can initially write the file in reversed order and then read one line at the time from the file's end (and write it to another file) with file-read-backwards module

1
  • I didn’t see this answer before, but it’s the same as mine. If I get the chance I’ll update this one to merge them with combined details.
    – owengall
    Aug 13 at 14:38
0

An improvement over the existing solution provided by @eyquem is as below:

def prepend_text(filename: Union[str, Path], text: str):
    with fileinput.input(filename, inplace=True) as file:
        for line in file:
            if file.isfirstline():
                print(text)
            print(line, end="")

It is typed, neat, more readable, and uses some improvements python got in recent years like context managers :)

0

I tried a different approach:

I wrote first line into a header.csv file. body.csv was the second file. Used Windows type command to concatenate them one by one into final.csv.

import os

os.system('type c:\\\header.csv c:\\\body.csv > c:\\\final.csv')
0

I believe (assuming you want to handle files too big to fit entire contents in memory) it would be easier to write in append mode (write order ascending), and have client/subscriber programs read using the file-read-backwards library.

In other words, simulate the file being prepended by the writer by reading the file lines in reverse order. This is my preferred solution since I don’t need the file to be stored in reverse order; I just want to be able to quickly access the newest line when reading it subsequently.

-1
with open("fruits.txt", "r+") as file:
    file.write("bab111y")
    file.seek(0)
    content = file.read()
    print(content)
2
  • 1
    This is not correct. Instead of appending to the start of the file, the first 7 characters of the file will be REPLACED with "bab111y". Jul 30, 2021 at 18:07
  • It can be useful if you know, or you can estimate the size of what to you want to write at the beginning. One can simply leave enough space to do it... Maybe just need to be completed to explain this fact.
    – Stefano
    Apr 6 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.