Python four day

2017-10-29  本文已影响9人  Adapa

文件操作

1.打开文件,得到文件句柄并赋值给一个变量
2.通过句柄对文件进行操作
3.关闭文件

r读
w写
a追加


    'r'       open for reading (default)
    'w'       open for writing, truncating the file first
    'x'       create a new file and open it for writing
    'a'       open for writing, appending to the end of the file if it exists
    'b'       binary mode
    't'       text mode (default)
    '+'       open a disk file for updating (reading and writing)
    'U'       universal newline mode (deprecated)

读文件:

data = open('小重山.txt','r',encoding='utf-8') #对象
print(data.read())  #读对象
data.close() #操作完成后关闭文件

创建文件和覆盖文件:

#如果有就写入new 内容覆盖
#如果没有就创建
data = open('小重山','w',encoding='utf-8')#
data.write('Hello world你好')
data.close()

文件内容追加

data = open('小重山.txt','a',encoding='utf-8')#追加模式APPEND
data.write('\n 夏侯淳哈哈哈')

data.close()

F操作

1.readline

data = open('小重山.txt','r',encoding='utf-8')
print(data.readline())#打印第一行
print(data.readline())#打印第一行的下一行
print(data.readline())#打印第一行的下一行的下一行
data.close()
>>>
 Hi,Good morning!

 How are you?

 I'm fine ,thanks.And you?

2.readlines
列表打印

data = open('小重山.txt','r',encoding='utf-8')
print(data.readlines())#以列表输出
data.close()
>>>
[' Hi,Good morning!\n', ' How are you?\n', " I'm fine ,thanks.And you?\n", " I'm ok.\n", ' Have a nice day!\n', ' you too.\n', ' Bay\n', ' 朝辞白帝彩云间,千里江陵一日还。\n', ' 穷寇莫子\n', ' 夏侯淳哈哈哈\n', ' 夏侯淳哈哈哈']

NEW

data = open('小重山.txt','r',encoding='utf-8')
li=data.readlines()
# print(li)#以列表输出

mumber =0
for i in li:
    mumber+=1
    if mumber ==6:
        i=''.join([i.strip(),'iiii'])#取代加号,节省内存
    print(i.strip())
data.close()

标准用法(节约内存)

data = open('小重山.txt','r',encoding='utf-8')

for i in data:
    print(i.strip())

data.close()

tell方法 (检测当前光标的位置)

data = open('小重山.txt','r',encoding='utf-8')

print(data.tell())
print(data.read(10))
print(data.tell())
#tell English 占用一个字符 Chinegs占用3个字符

data.close()
>>>
0
 Hi,Good m
10

seek调整光标位置

data = open('小重山.txt','r',encoding='utf-8')

print(data.tell())
print(data.read(10))
print(data.tell())
#tell English 占用一个字符 Chinegs占用3个字符
data.seek(2)#调整光标位置
print(data.tell())
print(data.read(5))
print(data.tell())
data.close()
>>>
0
 Hi,Good m
10
2
i,Goo
7

flush方法 (实时写入)

x = open('xxx','w',encoding='utf-8')
x.write('bibibibibi')
x.write('\n')
x.write('hehehehehehehe')
x.flush()#实时写入
flush

01进度条

import sys,time

for i in range(30):
    sys.stdout.write('x') #输出到控制台
    sys.stdout.flush() #实时刷新  内存-》硬盘
    time.sleep(0.1)

02进度条

import time
for i in range(30):
    print('*',end='',flush=True)
    time.sleep(0.1)

truncate()截短
a模式

x = open('小重山.txt','a',encoding='utf-8')
x.truncate(60)
x.close()

NEW模式

a+ 读写模式
w+ 写读模式
a+ 加读模式

a+

f = open('小重山','r+',encoding='utf-8') # r+读写模式
print(f.readline().strip())
f.write('BIBI')
print(f.read())
f.close()
>>>
Hellow mesat
Good af
sd
sd

小重山源文件

Hellow mesat
Good af
sd
sd

小重山new文件

Hellow mesat
Good af
sd
sdBIBI

w+
一开就就吧元数据清空了(光标后移)

f = open('小重山','w+',encoding='utf-8') # w+写读模式
print(f.read().strip())
f.write('BIBI')
print(f.read().strip())
f.close()

a+

f = open('小重山','a+',encoding='utf-8') # w+写读模式
print(f.read().strip())
f.write('BIBIBIBIBIBIBIBI')
f.seek(0)
print(f.read().strip())
f.close()

with方法

with open('小重山','r') as f:
    f.readline()
    f.read()
#只要运行过with里面语句就会关闭 避免人为错误的BUG
print('HEllo')

案列:

with open('小重山.txt','r') as f_read ,open('小重山','w') as f_write:
    name = 0
    for i in f_read:
        name+=1
        if name == 6:
            i = ''.join([i,'append'])
        f_write.write(i)

作业三级菜单(文件存储到file里面)

上一篇 下一篇

猜你喜欢

热点阅读