python第五章:文件--小白博客
文件操作, 操作文件完毕后一定要记得close
# 读,默认是rt(文本的方式读取),rb模式是以字节读取
# 文件路径可以用3中形式表示
f = open(r'C:\Users\fengzi\Desktop\firewalld.txt', 'rb')
f = open('C:\\Users\\fengzi\\Desktop\\firewalld.txt', 'rt', encoding='utf-8')
f = open('C:/Users/fengzi/Desktop/firewalld.txt', 'rt', encoding='utf-8')
f.read() #读取所有内容,光标移动到文件末尾
f.readline() #读取一行内容,光标移动到第二行首部
f.readlines() #读取每一行内容,存放于列表中
print(f.read().decode('utf-8'))
f.close()
# 写,默认是wt(文本的方式写入,覆盖写入
f.write('1111\n222\n') #针对文本模式的写,需要自己写换行符
f.write('1111\n222\n'.encode('utf-8')) #针对b模式的写,需要自己写换行符
f.writelines(['333\n','444\n']) #文件模式
例:
f = open('a.txt', 'w', encoding='utf-8')
f.write('申晨林是个好姑娘!\n其实是假的!')#\n是换行转译符
f.close()
# 以bytes类型写入文件(覆盖写入)
f = open('a.txt', 'wb')
f.write(b'')
f.close()
# 追加append(不覆盖,添加内容)
f = open('a.txt', 'a', encoding='utf-8')
f.write('你好')
f.close()
# 修改
f = open('test.txt', 'r', encoding='utf-8')
data = f.read()
if '' in data:
result = data.replace('', '')
f.close()
f = open('test.txt', 'w', encoding='utf-8')
f.write(result)
f.close()
# 另一种打开文件的方式,利用上下文
with open('a.txt', 'r', encoding='utf-8') as f:
print(f.read())
# 读取的类型
with open('a.txt', 'r', encoding='utf-8') as f:
print(f.read())
print(f.readline())#以行模式读取
for i in f:
print(i)
# 写入的类型
with open('a.txt', 'a', encoding='utf-8') as f:
f.write('aaaa')
f.writelines(['','','',''])#以列表的形式写入
#了解
f.readable() #文件是否可读
f.writable() #文件是否可读
f.closed #关闭文件
f.flush() #立刻将文件内容从内存刷到硬盘
f.name #查看打开的文件名 # 光标的移动(在文本模式seek前面的数字代表字符,字节模式seek前面的数字代表字节)
with open('a.txt', 'rb') as f:
f.seek(, )#等价于f.seek() # 代表把光标移动到开头
f.seek(, ) # 代表在相对位置移动2个字节(1代表光标的相对位置,2代表在相对位置上把光标向后移动2个字节)
f.seek(-, ) # 代表在末尾往前移动3个字节(2代表把光标移动到末尾,-3代表把光标向前移动3个字节)
f.read()#代表读取3个字符(意思是光标在第三个字节后面)
print(f.read())
练习
#动态查看文件
#tail -f message
import time
with open('a.txt', 'rb') as f:
f.seek(,)
while True:
data = f.readline()
if b'' in data:
print(data.decode('utf-8'))
else:
time.sleep(0.5) with open('a.txt', 'a', encoding='utf-8') as f:
f.write('500')
#作业题答案 made in zhou
# 第一题
# 编写一个用户登录程序
# 1、登录成功显示欢迎页面
# 2、登录失败显示密码错误,并显示错误几次
# 3、登录三次失败后,退出程序
username = 'root'
password = ''
count =
while count<:
a = input('name:')
b = input('pswd:')
if a==username and b==password:
print('yes')
break
else:
print('re')
count+=
if count==:
print('out')
# 第二题
# 可以支持多个用户登录
# 用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态
#(半成品)
info = {
'root':['root',],
'zhou':['zhou',],
'a':['a',]
}
sys = []
sel = int(input('请选择1:登录,2:注册'))
if sel==:
while True:
name = input('请输入用户名:')
sec = input('请输入新密码:')
sec2 = input('再次输入密码:')
if sec==sec2:
print('用户注册成功')
break
else:
print('两次密码不一致,重新输入')
if name and sec:
info = ['name',['sec',]]
# sys.append(info)
with open('test.txt','a',encoding=('utf-8')) as h:
h.write('\n%s:%s\n' %( info[],info[]))
#第一题答案
username = 'root'
password = 'root'
count =
print('请登录...')
while True:
user = input('username:')
pwd = input('password:')
if user == username and pwd == password:
print('欢迎登陆')
break
else:
count +=
print('密码错误', count)
if count == :
print('滚')
break
# 第二题答案
userinfo = {
'root':['root',],
'fengzi':['fengzi',],
'test': ['test',]
}
while True:
with open('lock.txt', 'r', encoding='utf-8') as f:
username = input('username:')
if not username:
continue
elif username in f.read():
print('您的账户已被锁定,请联系管理员')
elif username in userinfo.keys():
password = input('password:')
if password in userinfo[username][]:
print('欢迎页面')
break
else:
userinfo[username][] +=
print('密码错误',userinfo[username][])
if userinfo[username][] >= :
with open('lock.txt', 'a', encoding='utf-8') as f:
f.write('%s|' % username)
else:
print('用户名不存在')
python第五章:文件--小白博客的更多相关文章
- python四:函数练习--小白博客
为什么要有函数?函数式编程定义一次,多出调用函数在一定程度上可以理解为变量函数的内存地址加上()就是调用函数本身也可以当做参数去传参 不用函数:组织结构不清晰代码的重复性 def test():#te ...
- python第八章:多任务--小白博客
多线程threading 多线程特点: #线程的并发是利用cpu上下文的切换(是并发,不是并行)#多线程执行的顺序是无序的#多线程共享全局变量#线程是继承在进程里的,没有进程就没有线程#GIL全局解释 ...
- Python第五章__模块介绍,常用内置模块
Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群 群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...
- 简学Python第五章__模块介绍,常用内置模块
Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群 群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...
- [Python爬虫笔记][随意找个博客入门(一)]
[Python爬虫笔记][随意找个博客入门(一)] 标签(空格分隔): Python 爬虫 2016年暑假 来源博客:挣脱不足与蒙昧 1.简单的爬取特定url的html代码 import urllib ...
- perl5 第五章 文件读写
第五章 文件读写 by flamephoenix 一.打开.关闭文件二.读文件三.写文件四.判断文件状态五.命令行参数六.打开管道 一.打开.关闭文件 语法为open (filevar, file ...
- python实现的文本编辑器 - Skycrab - 博客频道 - CSDN.NET
Download Qt, the cross-platform application framework | Qt Project Qt 5.2.1 for Windows 64-bit (VS 2 ...
- python—webshell_醉清风xf_新浪博客
python—webshell_醉清风xf_新浪博客 python—webshell (2012-05-23 09:55:46) 转载▼
- Python第五天 文件访问 for循环访问文件 while循环访问文件 字符串的startswith函数和split函数 linecache模块
Python第五天 文件访问 for循环访问文件 while循环访问文件 字符串的startswith函数和split函数 linecache模块 目录 Pycharm使用技巧( ...
随机推荐
- Java代码优化总结(持续更新)
1.对equals不熟 例子 if(user.get("s").equals("ss")){ //一堆代码 } 注:一旦前端页面传null值过来,就错了,nul ...
- JMS Session session = connection.createSession(paramA,paramB) 两个参数不同组合下的含义和区别
Session session = connection.createSession(paramA,paramB); paramA是设置事务,paramB是设置acknowledgment mode ...
- mysql性能排查思路
mysql性能瓶颈排查 top/free/vmstat/sar/mpstat 查看mysqld进程的cpu消耗占比 确认mysql进程的cpu消耗是%user, 还是sys%高 确认是否是物理内存 ...
- Linux唤醒抢占----Linux进程的管理与调度(二十三)
1. 唤醒抢占 当在try_to_wake_up/wake_up_process和wake_up_new_task中唤醒进程时, 内核使用全局check_preempt_curr看看是否进程可以抢占当 ...
- c/c++ 字节对齐
c 字节对齐 概念: 结构体里会包括各种类型的成员,比如int char long等等,它们要占用的空间不同,系统为一个结构体开辟内存空间时,会有2种选择. 第一种:节省空间的方案,以上面的列子来说的 ...
- 用好lua+unity,让性能飞起来——lua与c#交互篇
前言 在看了uwa之前发布的<Unity项目常见Lua解决方案性能比较>,决定动手写一篇关于lua+unity方案的性能优化文. 整合lua是目前最强大的unity热更新方案,毕竟这是唯一 ...
- [Hive_add_1] Hive 与 MR 的对应关系
- windows下数据库文件使用脚本同步到linux下的mysql数据库中
1.背景 windows server 2008 下 每天会有 *.sql数据文件 需要上传到linux 中的mysql数据库中 而运维人员是在 windows server 下使用 xshell 连 ...
- February 23rd, 2018 Week 8th Friday
It takes a strong man to save himself, and a great man to save another. 强者自救,圣者渡人. When you are not ...
- android开发——用户头像
最近,小灵狐得知了一种能够加快修炼速度的绝世秘法,那便是修炼android神功.小灵狐打算用android神功做一个app,今天他的修炼内容就是头像功能.可是小灵狐是个android小白啊,所以修炼过 ...