Why does my text file keep overwriting the data on it?

w overwrites, open with a to append or open the file once outside the loop:

append:

while True:
    posts = requests.get(posts['paging']['next']).json()
    #print posts
    with open('test121.txt', 'a') as outfile:
        json.dump(posts, outfile)

Open once outside the loop:

with open('test121.txt', 'w') as outfile:
    while True:
        posts = requests.get(posts['paging']['next']).json()
        #print posts
        json.dump(posts, outfile)

It makes more sense to use the second option, if you are going to be running the code multiple times then you can open with a outside the loop also, if the file does not exist it will be created, if it does data will be appended


This is because you are using the file operator with w mode, you are overwriting the content. You can use the a append mode:

It can be done like this

Modification:

with open('test121.txt', 'w') as outfile:
   while True:
       posts = requests.get(posts['paging']['next']).json() 
       json.dump(posts, outfile)

w overwrites on the existing file

i.e)

File1.txt:

123

code:

with open("File1.txt","w") as oup1:
    oup1.write("2")

File1.txt after python run:

2

Its value is overwritten

a appends to the existing file

i.e)

File1.txt:

123

code:

with open("File1.txt","a") as oup1:
    oup1.write("2")

File1.txt after python run:

1232

The written content is appended to the end.


Opening and Closing Files

to use actual data files reading and writing to the standard input and output.

Python provides basic functions and methods necessary to manipulate files by default. You can do your most of the file manipulation using a file object.

The open Function

Before you can read or write a file, you have to open it using Python's built-in open() function. This function creates a file object, which would be utilized to call other support methods associated with it.

Syntax file object = open(file_name [, access_mode][, buffering])

Here are parameter details:

file_name: The file_name argument is a string value that contains the name of the file that you want to access.

access_mode: The access_mode determines the mode in which the file has to be opened, i.e., read, write, append, etc. A complete list of possible values is given below in the table. This is optional parameter and the default file access mode is read (r).

buffering: If the buffering value is set to 0, no buffering takes place. If the buffering value is 1, line buffering is performed while accessing a file. If you specify the buffering value as an integer greater than 1, then buffering action is performed with the indicated buffer size. If negative, the buffer size is the system default(default behavior).

Here is a list of the different modes of opening a file −

Modes and Description r= Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.

rb= Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.

r+= Opens a file for both reading and writing. The file pointer placed at the beginning of the file.

rb+= Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file.

w= Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

wb= Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

w+= Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

wb+= Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

a= Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

ab= Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

a+= Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

ab+= Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

Reading and Writing Files

The file object provides a set of access methods to make our lives easie with the use of read() and write() methods to read and write files.

The write() Method

The write() method writes any string to an open file. It is important to note that Python strings can have binary data and not just text.

The write() method does not add a newline character ('\n') to the end of the string −

Syntax

fileObject.write(string);

Here, passed parameter is the content to be written into the opened file.

Example

# Open a file
fo = open("file.txt", "wb")
fo.write( "Python is a great language");
# Closeopend file
fo.close() 

The above method would create foo.txt file and would write given content in that file and finally it would close that file. If you would open this file, it would have following content.

Python is a great language.

The read() Method

The read() method reads a string from an open file. It is important to note that Python strings can have binary data. apart from text data.

Syntax

 fileObject.read([count]);

Here, passed parameter is the number of bytes to be read from the opened file. This method starts reading from the beginning of the file and if count is missing, then it tries to read as much as possible, maybe until the end of file.

Example

Let's take a file foo.txt, which we created above.

 # Open a file
    fo = open("foo.txt", "r+")
    str = fo.read(10);
    print "Read String is : ", str
    # Close opend file
    fo.close()

This produces the following result − Read String is : Python is