Python four day

文件操作

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里面)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 目录(?)[-] 关于Vim 1 Vim的几种模式 启动Vim 文档操作 光标的移动 1 基本移动 2 翻屏 3 ...
    SunnyLeong阅读 8,753评论 0 32
  • 1.能调用方法的一定是对象,比如数值、字符串、列表、元组、字典,甚至文件也是对象,Python中一切皆为对象。 s...
    ph_郭先森阅读 987评论 0 0
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,587评论 9 467
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,981评论 19 139
  • React是个非常适合组件化开发的框架,可以说facebook重新定义了前端。但项目中实际使用下来,维护各组件的s...
    张歆琳阅读 1,528评论 3 8