python---文件和json

一.文件:

hello boy
nice to meet you

操作文件的函数/方法
在python 中要操作文件需要记住的1个函数和三个方法
#python中一切节对象
open : 打开文件,并且返回文件操作对象
read : 将文件内容读取到内存
write : 将指定内容写到文件
close : 关闭文件
open 函数负责打开文件,并且返回文件对象
read/write/close 三个方法都需要通过文件对象来调用

read方法–读取文件
open函数的第一个参数是要打开的文件名(文件名区分大小写)
如果文件存在,返回文件操作对象
如果文件不存在,则不返回
read方法可以一次性读开返回文件的所有内容
close 方法负责关闭文件

“”“

# 1.打开文件
txt = open('ReadMe')
# 2.操作文件 读/写
# read方法:读取文件内容(一次性返回所有的内容)
text = txt.read()
print text
# 3.关闭文件
# close() 方法: 负责关闭文件。
txt.close()

结果:
hello boy
nice to meet yo

文件指针:
在第一次读完文件之后,在读一次,发现没有输出读取的文件,证明存在文件
指针,当第一次读完之后,指针到最后一行,所以无法读取

“”“

txt = open('ReadMe')
text = txt.read()
print text
print '*' * 50
text = txt.read()
print text
txt.close()

结果:
hello boy
nice to meet you


这个实验证明文件指针确实存在
第一次打印text时,可以打印出字长
第二次打印text时,打印不出来
证明文件指针确实存在

txt = open('ReadMe')
text = txt.read()
print text
print type(txt)
print len(text)

print '*' *50
text = txt.read()
print len(text)
txt.close()

结果:
hello boy
nice to meet you

txt = open('/home/kiosk/Desktop/file1')
text = txt.read()
print text
print len(text)
txt.close()

结果:
xixi
xixix
xixixi
hehe

23

写:
覆盖:
变量名 = open(‘文件名’,‘w’)
以新写的文件内容将以前写的内容覆盖掉

"""
# 1.打开文件
txt = open('ReadMe','w')
# 2.写入文件
txt.write('hello')
# 3.关闭文件
txt.close()

会直接将文件中的东西全部覆盖掉,以前的东西全部删除

以追加方式打开文件
如果该文件存在,文件指针会放在文件的末尾
如果文件不存在,创建文件并写入

“”“

# 1.打开文件
txt = open('ReadMe','a')
# 2.写入文件
txt.write('\n这是追加的哦~')
# 3.关闭文件
txt.close()

按行读取文件
read 方法默认会把文件的所有内容一次性读到内存
如果文件太大,对内存的占用会非常严重
readline方法:
readline方法可以一次性对去一些内容
方法执行后,会把文件指针以东到下一行,准备再次读取

“”“

# 怎么读取大文件
file = open('ReadMe')
# 为什么要写成四循环:因为我们不知道要读取的文件有多少行
while True:
    text =file.readline()
    # 如果文件指针到文件最后一行,那么就读不到内容了
    if not text:
        break
        # 每读取以行,末尾都已经有一个\n
    print text
file.close()

结果:
hello

这是追加的哦~

这是追加的哦~

这是追加的哦~

这是追加的哦~

这是追加的哦~

这是追加的哦~

复制:

# 1打开文件
# 源文件以只读的方式打开
file_read = open('ReadMe')
# 目标文件以写的方式打开
file_write = open('ReadMe_1','w')
# 从源文件中读取内容
text = file_read.read()
# 将读取到的内容写道目标文件中
file_write.write(text)

#关闭文件
file_read.close()
file_write.close()

大文件的复制:
# 1打开文件
file_read = open('ReadMe')
file_write = open('ReadMe_2','w')
# 2.复制文件
while True:
    text = file_read.readline()
    if not text:
         break
    print text
    file_write.write(text)
# 3.关闭文件
file_read.close()
file_write.close()

文件2:

01:

关键字with在不需要访问文件后将其关闭,在这个程序中,
我们调用open(),但没有调用close();你也可以调用
open()和close来打开和关闭文件,但这样做时,
如果程序存在bug,导致close()语句没有执行,
文件将不会关闭,未妥善关闭文件可能会导致数据丢失或受损,
如果在程序中过早地调用close(),
你会发现需要使用文件时它已经关闭(无法访问),
这回导致更多地错误,你并非在任何情况下都能轻松地确定关闭文件地恰当时间
通过使用with结构,可让python去确定,
你只管打开文件,并在需要时使用它
python会在和是的时候自动将其关闭

“”“

with open('file1') as file_object:
    contents = file_object.read()
    print contents

02:

filename = 'file1'
with open(filename) as file_object:
    for line in file_object:
        print line

03:

file = 'linux'
with open(file,'w') as file_object:
    file_object.write('I love jinx\n')
    file_object.write('I love leesen\n')

04:

file = 'linux'
with open(file,'a') as file_object:
    file_object.write('I love jax\n')
    file_object.write('I hate you')

二.json:

很多程序都要求用户输入某种信息,
程序都把用户提供的信息存储在列表和字典等数据结构中,
用户关闭程序时,你几乎总是要保存他们的信息:
一种简单的方式是使用模块json来存储数据
(在python中使用json的时候,主要也就是使用json模块
json是以一种良好的格式来进行数据的交互)
模块json让你能够将简单的python数据结构转存到文件中,
并在程序再次运行时家在该文件中的数据,
更重要的是,json数据格式并非python专用的,
这让你能够将json格式存储的数据与使用其他变成语言的人分享

注意:
json(javascriptobect notation)格式最初时为javascript开发的
,但虽有成了一种常见格式,被很多种语言来使用

01:

import json
number = [1,2,3,4]
with open('numbers.json','w') as f_josn:
    json.dump(number,f_josn)

02:

import  json
filename = 'numbers.json'
with open(filename) as j_json:
    numbers = json.load(j_json)
print numbers

03:

import json
username = raw_input('what is your name ?')
filename = 'username.json'
with open(filename,'a') as j_json:
    json.dump(username,j_json)
    print 'we will rember you when you back %s' %username

04:

import json
filename = 'username.json'
with open(filename) as j_josn:
    username = json.load(j_josn)
    print 'welcome back ,%s' %username

05综合应用:

import json
filename = "usrename.json"
try:
    with open(filename) as j_json:
        username = json.load(j_json)
except ValueError:
    username = raw_input('what is your name?')
    with open(filename,'w') as j_json:
        json.dump(username,j_json)
        print 'we will remeber you when you come back %s' %username
# 依赖于try代码块成功执行的代码都应该放到else代码块中:
else:
    print 'welcome back %s' %username

猜你喜欢

转载自blog.csdn.net/a939029674/article/details/81219094
今日推荐