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使用技巧( ...
随机推荐
- CentOS6.5内 Oracle 11GR2静默安装
一.修改配置文件 1.1.修改/etc/security/limits.conf文件,修改用户的SHELL的限制. 输入命令:vi /etc/security/limits.conf,将下列内容加入该 ...
- SqlServer执行Insert命令同时判断目标表中是否存在目标数据
针对于已查询出数据结果, 且在程序中执行Sql命令, 而非数据库中的存储过程 INSERT INTO TableName (Column1, Column2, Column3, Column4, Co ...
- C#获取一个实体类的属性名称、属性值
using System.Reflection; Type t = obj.GetType();//获得该类的Type foreach (PropertyInfo pi in t.GetPropert ...
- python3+正则表达式爬取 猫眼电影
'''Request+正则表达式抓取猫眼电影TOP100内容''' import requests from requests.exceptions import RequestException i ...
- CentOS 7.0下安装Python3.6
CentOS 7.0自带Python2.7 安装Python3.6步骤 1.安装依赖 yum install -y zlib-devel bzip2-devel openssl-devel ncurs ...
- June 18. 2018, Week 25th. Monday
Health and cheerfulness naturally beget each other. 安康喜乐,相生相成. From Joseph Addison. Good health is a ...
- Angular四大核心特性
Angular四大核心特性 Angular四大核心特性理论概述 MVC模式:它目的是为了分离视图.模型和控制器而设计出来的:其中数据模型用来储存数据,视图用来向用户展示应用程序,控制器充当模型和视图之 ...
- SpringCloud之初识Robbin---负载均衡
在上一篇中讲解Eureka注册中心的案例,我们启动了一个user-service,然后通过DiscoveryClient来获取服务实例信息,然后获取ip和端口来访问. 但是实际环境中,我们往往会开启很 ...
- SQlite源码分析-体系结构
体系结构 在内部,SQLite由以下几个组件组成:内核.SQL编译器.后端以及附件.SQLite通过利用虚拟机和虚拟数据库引擎(VDBE),使调试.修改和扩展SQLite的内核变得更加方便.所有SQL ...
- cocos2d-x 绘制图形
转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/25841727 绘制图形例如以下: 程序代码: 须要又一次定义父类虚函数draw() ...