首页 > 编程语言 > 详细

python脱产4期内容整理NO.8

时间:2018-09-20 20:50:58      阅读:179      评论:0      收藏:0      [点我收藏+]

一,文件处理的模式基本概念

            打开文件的模式有三种:r(默认),w ,a

            控制操作文件内容格式的两种模式:t(默认),b

            操作文件内容格式不能单独使用,必须与纯净模式结合

二,打开文件模式 详解

           1.r是只读模式:文件不存在时会报错,文件存在时文件指针位于文件开头部分

                  with open(‘a.txt‘,mode=’rt‘,encoding=’utf-8‘)as f:

                       print(f.readlines())

           2.w是只写模式:在文件不存在时,会建立新文件,文件存在时内容会删除旧文件,

              文件指针在文件开头

                     with open(‘a.txt‘,mode=’rt‘,encoding=’utf-8‘)as f:

                      print(f.writable())

                      print(f.readable())

                      print(f.writelb‘你好\n‘)

                     print(f.writeble‘我好,\n’)

                     print(f.writelb‘大家好,\n’)

         注册功能:

               

         name=input(‘username>>>:‘).strip()
ppwd=input(‘password>>>:‘).strip()
with open(‘abl.txt‘,mode=‘at‘,encoding=‘utf-8‘) as f:
info=‘%s:%s\n‘%(name,pwd)
f.write(info)

3.a之追加写模式:在文件不存在时会创建空文件,存在时文件指针会停留在末尾
with open(‘c.txt’,mode=‘at’,endoding=utf-8‘) as f:
f.write(‘111\n‘)
f.write(‘222\n‘)
print(f.readable())
print(f.writable)

r+ w+ a+
with open(‘a.txt‘,mode=‘r+t‘,encoding=‘utf-8) as f
print(f.readable())
print(f.writable())
print(f.readline())
f.write(‘你好’)

b读写都是以二进制为单位
with open(‘b.txt‘,mode=’ab‘)as f
data=f.read()
#print(data,type(data))
print(data.decode(’utf-8‘))

with open(’1.png‘,mode=’rb‘)as f
data=f.read()
print(data)

拷贝工具
src_file=input(‘源文件路径:‘).strip()
dst_file=input(’目标文件:‘).strip()
with open(r‘%s % src_file,mode=‘rb‘) us read(r‘%s % dst_file,mode=‘wb‘) as write_f:
for line in read_f
write_f.write(line)




 

        

 

python脱产4期内容整理NO.8

原文:https://www.cnblogs.com/wuzhengzheng/p/9683086.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!