Why is my text file overwriting data on it?

I am trying to pull some data from facebook pages for a product and upload it all to a text file, but I found that the file continues to overwrite itself with data. I'm not sure if this is a pagination problem, or if I need to make multiple files.

Here is 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 why this is happening?

+5
source share
3 answers

This use to use a w mode file operator you rewrite content that you can use a append method

This 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 overwrite existing file

those.)

FILE1.TXT:

 123 

code:

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

File1.txt after running python:

 2 

That is, this value is overwritten

a adds to an existing file

those.)

FILE1.TXT:

 123 

code:

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

File1.txt after running python:

 1232 

Written content is added to the end.

+3
source

w overwrites, opens with a to add or open a file 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 sense to use the second option, if you are going to run the code several times, then you can open with a outside the loop as well, if the file does not exist, it will be created if data is added

+5
source

Opening and closing files

Use the actual data files for reading and writing for standard input and output.

Python provides the basic functions and methods required to work with files by default. You can do most of the file manipulation using a file object.

Open function

Before you can read or write a file, you need to open it using the Python built-in open () function. This function creates a file object that will be used to call other support methods associated with it.

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

Here are the options:

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

access_mode:. access_mode defines the mode in which the file should be opened, i.e. read, write, add, etc. A complete list of possible values ​​is given in the table below. This is an optional parameter, and the default access mode is read (r).

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

Here is a list of different ways to open a file -

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

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

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

rb + = Opens a file for reading and writing in binary format. The file pointer is 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, a new file is created for writing.

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

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

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

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

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

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

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

Reading and writing files

A file object provides a set of access methods that make our life easier using the read () and write () methods to read and write files.

Write () method

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

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

Syntax

 fileObject.write(string); 

Here the passed parameter is the content that should be written to the open file.

Example

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

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

Python is a great language.

Read () method

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

Syntax

  fileObject.read([count]); 

Here, the parameter passed is the number of bytes that should be read from the open file. This method starts reading from the beginning of the file, and if there is no account, then it tries to read as much as possible, possibly to the end of the file.

Example

Take the foo.txt file 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 gives the following result: Reading a line: Python

+2
source

All Articles