为什么我的文本文件会覆盖它上面的数据?

sem*_*lex 5 python json

我试图从facebook页面中提取一些产品的数据并将其全部转储到文本文件中,但我发现该文件会一直覆盖数据.我不确定这是一个分页问题还是我必须制作几个文件.

这是我的代码:

#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)
Run Code Online (Sandbox Code Playgroud)

知道为什么会这样吗?

Pad*_*ham 5

w覆盖,打开以a在循环外部追加或打开文件:

附加:

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

在循环外打开一次:

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

使用第二个选项更有意义,如果您要多次运行代码,那么您也可以a在循环外部打开,如果文件不存在则会创建它,如果它确实会附加数据


The*_*nse 3

这是因为您正在使用带有模式的文件运算符w,您正在覆盖内容。您可以使用a附加模式:

可以这样做

修改:

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

w覆盖现有文件

IE)

文件1.txt:

123
Run Code Online (Sandbox Code Playgroud)

代码:

with open("File1.txt","w") as oup1:
    oup1.write("2")
Run Code Online (Sandbox Code Playgroud)

python运行后的File1.txt:

2
Run Code Online (Sandbox Code Playgroud)

它的值被覆盖

a附加到现有文件

IE)

文件1.txt:

123
Run Code Online (Sandbox Code Playgroud)

代码:

with open("File1.txt","a") as oup1:
    oup1.write("2")
Run Code Online (Sandbox Code Playgroud)

python运行后的File1.txt:

1232
Run Code Online (Sandbox Code Playgroud)

书面内容附在最后。