Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

json

I'm trying to pull some data from Facebook pages for a product and dump it all into a text file, but I find that the file keeps overwriting itself with the data. I'm not sure if it's a pagination issue or if I have to make several files.

Here's my code:

#Modules
import requests
import facebook
import json

def some_action(post):
    print posts['data']
    print post['created_time']

#Token
access_token = 'INSERT ACCESS TOKEN'
user = 'walkers'

#Posts
graph = facebook.GraphAPI(access_token)
profile = graph.get_object(user)
posts = graph.get_connections(profile['id'], 'posts')

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

Any idea as to why this is happening?

like image 552
semiflex Avatar asked Aug 05 '15 09:08

semiflex


People also ask

How can I write data to a text file without overwriting the current data in C#?

TextWriter tsw = new StreamWriter(@"C:\Hello. txt"); //Writing text to the file. tsw. WriteLine("Hello"); //Close the file.

What mode overwrite existing text in a file?

To overwrite a file, to write new content into a file, we have to open our file in “w” mode, which is the write mode. It will delete the existing content from a file first; then, we can write new content and save it. We have a new file with the name “myFile. txt”.

What is overwriting of data?

Data erasure (sometimes referred to as data clearing, data wiping, or data destruction) is a software-based method of overwriting the data that aims to completely destroy all electronic data residing on a hard disk drive or other digital media by using zeros and ones to overwrite data onto all sectors of the device in ...


2 Answers

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

like image 82
Padraic Cunningham Avatar answered Nov 12 '22 06:11

Padraic Cunningham


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.

like image 26
The6thSense Avatar answered Nov 12 '22 05:11

The6thSense