文件操作, 操作文件完毕后一定要记得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第五章:文件--小白博客的更多相关文章

  1. python四:函数练习--小白博客

    为什么要有函数?函数式编程定义一次,多出调用函数在一定程度上可以理解为变量函数的内存地址加上()就是调用函数本身也可以当做参数去传参 不用函数:组织结构不清晰代码的重复性 def test():#te ...

  2. python第八章:多任务--小白博客

    多线程threading 多线程特点: #线程的并发是利用cpu上下文的切换(是并发,不是并行)#多线程执行的顺序是无序的#多线程共享全局变量#线程是继承在进程里的,没有进程就没有线程#GIL全局解释 ...

  3. Python第五章__模块介绍,常用内置模块

    Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群  群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...

  4. 简学Python第五章__模块介绍,常用内置模块

    Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群  群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...

  5. [Python爬虫笔记][随意找个博客入门(一)]

    [Python爬虫笔记][随意找个博客入门(一)] 标签(空格分隔): Python 爬虫 2016年暑假 来源博客:挣脱不足与蒙昧 1.简单的爬取特定url的html代码 import urllib ...

  6. perl5 第五章 文件读写

    第五章 文件读写 by flamephoenix 一.打开.关闭文件二.读文件三.写文件四.判断文件状态五.命令行参数六.打开管道 一.打开.关闭文件   语法为open (filevar, file ...

  7. python实现的文本编辑器 - Skycrab - 博客频道 - CSDN.NET

    Download Qt, the cross-platform application framework | Qt Project Qt 5.2.1 for Windows 64-bit (VS 2 ...

  8. python—webshell_醉清风xf_新浪博客

    python—webshell_醉清风xf_新浪博客 python—webshell (2012-05-23 09:55:46) 转载▼

  9. Python第五天 文件访问 for循环访问文件 while循环访问文件 字符串的startswith函数和split函数 linecache模块

    Python第五天   文件访问    for循环访问文件    while循环访问文件   字符串的startswith函数和split函数  linecache模块 目录 Pycharm使用技巧( ...

随机推荐

  1. ElementUI在IE11下兼容性修改

    1.在项目里面使用了axios.js来发送http请求,在IE下报错Promise未定义,解决办法: 到http://bluebirdjs.com/docs/getting-started.html  ...

  2. iis 6,7 ftp 进行用户隔离进行权限控制,不同用户查看不同文件夹

    iis 6 配置点击链接 http://www.jb51.net/article/20676.htm iis 7配置 1.建立文件夹 C:\ftp, 并增加 目录 localuser(这个是必须的名字 ...

  3. 转:修改IIS虚拟目录名称bat脚本

    @echo off echo ------------------------------------------------------------------------------ echo - ...

  4. Linux进程调度器概述--Linux进程的管理与调度(十五)

    调度器面对的情形就是这样, 其任务是在程序之间共享CPU时间, 创造并行执行的错觉, 该任务分为两个不同的部分, 其中一个涉及调度策略, 另外一个涉及上下文切换. 1 背景知识 1.1 什么是调度器 ...

  5. php中jpgraph库的使用

    用Jpgraph,只要了解它的一些内置函数,可以轻松得画出折线图.柱形图.饼状图等图表. 首先要保证PHP打开了Gd2的扩展: 打开PHP.ini,定位到extension=php_gd2.dll,把 ...

  6. firefox浏览器 插件--【维基百科+谷歌翻译】高级应用之 带图翻译

    [维基词典+谷歌翻译]插件地址: https://addons.mozilla.org/zh-CN/firefox/addon/google-dictionary-and-google-t/?src= ...

  7. SQL server 数据库的索引和视图、存储过程和触发器

    1.索引:数据排序的方法,快速查询数据 分类: 唯一索引:不允许有相同值 主键索引:自动创建的主键对应的索引,命令方式不可删 聚集索引:物理顺序与索引顺序一致,只能创建一个 非聚集索引:物理顺序与索引 ...

  8. 设置SSH免密码登录

    1.cd .ssh 2.执行下面的命令,三次回车. ssh-keygen -t rsa cat id_rsa.pub >> authorized_keys 3.发送公钥 scp .ssh/ ...

  9. chmod、acl权限

    R:4W:2X:1 chmod g+r file 给所有者的组内用户加上读的权限chmod o+r file 给其他组用户加上读的权限chmod a+x file 给所有用户加上写执行的权限chmod ...

  10. LeetCode算法题-Single Number(Java实现)

    这是悦乐书的第175次更新,第177篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第34题(顺位题号是136).给定一个非空的整数数组,除了一个元素外,每个元素都会出现两 ...