python基础语法—day11(笔记)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python基础语法—day11(笔记),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4298字,纯文字阅读大概需要7分钟。
内容图文
文章目录
主要内容:
1.不使用多态的问题
class Person(object):
def eat(self):
print('正在吃冰淇淋')
class Student(object):
def __init__(self,name,id:int):
self.name=name
self.id=id
def food(self):
self.id.eat()
p=Person()
s=Student('小明',p)
s.food()
# 正在吃冰淇淋
class Person1(object):
def eat(self):
print('正在吃冰淇淋')
class Person2(object):
def sleep(self):
print('正在睡觉')
class Student(object):
def __init__(self,name):
self.name=name
def food(self):
self.id.eat()
def action(self):
self.id.sleep()
p1=Person1()
s=Student('小明')
s.id=p1
s.food()
p2=Person2()
s.id=p2
s.action()
2.多态的使用
class Person(object):
def study(self):
print('正在学习')
class Person1(Person):
def study(self):
print('正在看报刊')
class Person2(Person):
def study(self):
print('正在做数学题')
class Person3(Person):
pass
class Student(object):
def __init__(self,name):
self.name=name
self.id=None
def stu(self):
if self.id is not None and isinstance(self.id,Person):
self.id.study()
s=Student('小明')
p1=Person1()
s.id=p1
s.stu()
p2=Person2()
s.id=p2
s.stu()
p3=Person3()
s.id=p3
s.stu()
# 正在看报刊
# 正在做数学题
# 正在学习
3.文件的打开和关闭
python里使用open内置函数打开并操作一个文件
open参数介绍:
1)file:用来指定打开的文件(不是文件的名字,而是文件的路径)
2)mode:打开文件时的模式,默认是 r ,表示只读
3)encoding:打开文件时的编码方式
在windows操作系统里,默认使用gbk编码格式打开文件。写入和读取需要相同的编码格式。操作完成以后要关闭文件
file=open('test2.txt',encoding='utf-8')
print(file.read())
file.close()
路径:
1)绝对路径:从电脑盘符开始的路径
路径书写的三种方式:1.\ 2.r’’ 3./ (推荐)
# file=open('C:\\Users\\lenovo\\untitled2\\text2.txt',encoding='utf-8')
# file=open(r'C:\Users\lenovo\untitled2\test2.txt',encoding='utf-8')
file=open('C:/Users/lenovo/untitled2/test2.txt',encoding='utf-8')
2)相对路径:当前文件所在的文件夹的开始路径(使用较多)
…/ 表示返回到上一级文件夹
./ 可以不写,表示当前文件夹
# file=open('test2.txt',encoding='utf-8')
# file=open('./test2.txt',encoding='utf-8')
file=open('../demo.txt',encoding='utf-8')
文件打开方式:
1)r:只读模式,默认,打开文件以后,只能读取,不能写入,如果文件不存在会报错。
读取:
file=open('test2.txt',encoding='utf-8')
print(file.read())
file.close()
写入会报错(io.UnsupportedOperation: not writable):
file=open('test2.txt',encoding='utf-8')
file.write('hello')
file.close()
# io.UnsupportedOperation: not writable
2)w:写入文件,打开文件以后,只能写入,不能读取,如果文件存在,会覆盖文件,如果文件不存在,会创建文件。
file = open('tes2.txt', 'w',encoding='utf-8')
file.write('hello')
file.close()
3)b:以二进制的形式打开文件,可以用来操作非文本文件
4)t:文本形式打开
5)rb:以二进制读取;wb:以二进制写入
file = open('test2.txt','rb')
print(file.read()) #b'\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x93\x88\xe5\x93\x88'
file.close()
file1=open('xxx.txt','wb')
file1.write('欢迎大家来到北京天安门'.encode('utf-8'))
file1.close()
6)a:追加模式,会在最后追加内容,如果文件不存在,会创建文件,如果文件存在,会追加,不会覆盖
7)r+和w+:都是可读写(用的不多)
file=open('test2.txt','w+',encoding='utf-8')
file.write('hello哈哈哈')
file.seek(0,0) #写入之后,文件指针到最后,需要调用seek将文件指针重置到开头
print(file.read()) #hello哈哈哈
file.close()
4.文件的读取方式
(1)file.read():将所有数据全都读出来,file.read(10)表示读取的长度
file=open('xxx.txt','rt',encoding='utf-8')
print(file.read(10)) #读取的长度
file.close()
file=open('F:/wdzp6.jpg','rb')
while True:
content=file.read(1024)
print(content)
if not content:
break
file.close()
(2)file.readline():只读取一行数据
file=open('xxx.txt','r',encoding='utf-8')
print(file.readline())
while True:
content=file.readline()
print(content)
if content=='':
break
file.close()
(3)file.readlines():读取所有行数据,保存在一个列表里
file=open('xxx.txt','r',encoding='utf-8')
print(file.readlines()) #['哈哈哈\n', '嘻嘻嘻\n', 'wuwuwu\n', '噫噫噫\n', '哔哔哔']
file.close()
5.文件拷贝功能
import os
filename=input('请输入一个文件的路径:')
if os.path.isfile(filename): #判断是否是文件
old_file=open(filename,'rb')
txt=filename.rpartition('.')
new_filename=txt[0]+'.bak.'+txt[2]
new_file=open(new_filename,'wb')
while True:
content=old_file.read(1024)
new_file.write(content)
if not content:
break
new_file.close()
old_file.close()
else:
print('您输入的文件不存在')
内容总结
以上是互联网集市为您收集整理的python基础语法—day11(笔记)全部内容,希望文章能够帮你解决python基础语法—day11(笔记)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。