Day17 Python课程学习内容
1. 文件的写入
- write() 可以向文件中写入内容
- 使用open()函数,如果不指定操作类型,则默认为读取文件,而读取文件时,无法在文件中写入内容。
- 使用w来写入文件的时候,如果文件不存在,它会创建文件。
- 如果文件存在,则会在该文件文件中有内容,则会覆盖原先所有内容
| 指令名称 | 指令含义 |
|---|---|
| r | 表示只可读取文件,读取文件 |
| w | 表示可读写文件,覆盖原内容 |
| a | 表示追加内容,不会覆盖原内容 |
| x | 新建文件,文件不存在创建,存在报错 |
'''demo.txt'''
Hello World!
Hello Python!
Hello Sam and Janice!

'''module.py'''
file_name = 'demo.txt'
with open(file_name, encoding = 'utf-8') as file_obj:
file_obj.write('Hello Mike!')
'''io.UnsupportedOperation: not writable''' # 报错,不支持此操作。
'''module.py'''
file_name = 'demo.txt'
with open(file_name, 'w', encoding = 'utf-8') as file_obj:
file_obj.write('Hello Python!')

- write 会返回数值,数值内容为写入内容的长度。
'''module.py'''
file_name = 'demo.txt'
with open(file_name, 'w', encoding = 'utf-8') as file_obj:
r = file_obj.write('Hello Python!')
print(r)

- write() 文件必须输入str字符串否则会报错。
file_name = 'demo.txt'
with open(file_name, 'w', encoding = 'utf-8') as file_obj:
file_obj.write(123) # 参数不行
'''
file_obj.write(123)
TypeError: write() argument must be str, not int
'''
file_obj.write(['1','2','3']) # 列表不行
'''
file_obj.write(['1','2','3'])
TypeError: write() argument must be str, not list
'''
........
'''demo.txt'''
Hello World!
Hello Python!
Hello Sam and Janice!

指令a ,追加内容
'''module.py'''
file_name = 'demo.txt'
with open(file_name, 'a', encoding = 'utf-8') as file_obj:
file_obj.write('\nHello Python!')

2.二进制文件的读写
- 创建一个二进制文件,例如:音乐MP3格式文件。
| 指令名称 | 指令含义 |
|---|---|
| t | 读取文本文件(默认值) |
| b | 读取二进制文件 |

'''module.py'''
file_name = r'C:\Users\Administrator\Desktop\The Dry Spells - Rise.mp3'
with open(file_name, 'r') as file_obj:
print(file_obj.read())
# UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 618: illegal multibyte sequence
with open(file_name, 'rb') as file_obj:
print(file_obj.read(100))

- 播放音乐
'''module.py'''
file_name = r'C:\Users\Administrator\Desktop\The Dry Spells - Rise.mp3'
with open(file_name, 'rb') as file_obj:
# 将读取的内容写出来
# 定义一个新的文件
new_name = 'Rise.mp3'
with open(new_name, 'wb') as new_obj:
# 定义读取的大小
chunk = 100 * 1024
while Ture:
content = file_obj.read(chunk)
if not content:
break
# 将读取到的新内容写入新的文件当中
new_obj.write(content)

3.super
# ---classroom case---
class Person:
def __init__(self):
self.name = 'Sam'
self.age = 18
def run(self):
print(self.name + ' is running.')
def eat(self):
print(self.name + ' is eating.')
class Student(Person):
def __init__(self):
print('__init__ in Student.')
class Teacher(Person):
pass
s = Student()
'''
__init__ in Student.
'''
s.run()
'''
print(self.name + ' is running.')
AttributeError: 'Student' object has no attribute 'name'
'''
# -----------------------------------------------------------------------
class Person:
def __init__(self):
self.name = 'Sam'
self.age = 18
def run(self):
print(self.name + ' is running.')
def eat(self):
print(self.name + ' is eating.')
class Student(Person):
def __init__(self):
print('__init__ in Student.')
class Teacher(Person):
pass
s = Student()
# __init__ in Student.
# ---------------------------------------------------------
# 在子类中要调用父类的init方法,得用super
class Person:
def __init__(self):
self.name = 'Sam'
self.age = 18
def run(self):
print(self.name + ' is running.')
def eat(self):
print(self.name + ' is eating.')
class Student(Person):
def __init__(self):
print('__init__ in Student.')
super().__init__()
class Teacher(Person):
pass
s = Student()
s.run()
'''
__init__ in Student.
Sam is running.
'''
# ----------------------
class Person:
def __init__(self,name):
self.name = name
self.age = 18
def run(self):
print(self.name + ' is running.')
def eat(self):
print(self.name + ' is eating.')
class Student(Person):
def __init__(self,name):
print('__init__ in Student.')
super().__init__(name)
class Teacher(Person):
pass
s = Student('Sam')
s.run()
'''
__init__ in Student.
Sam is running.
'''
t = Teacher()
'''
t = Teacher()
TypeError: __init__() missing 1 required positional argument: 'name'
''' # 需加入参数
t = Teacher('Janice')
- 如果子类需要重新定义一个新内容,需要在子类里面的init方法调用父类的init方法。然后单独写自己的方法。
class Person:
def __init__(self,name,age):
self.name = name
self.age = age
def run(self):
print(self.name + ' is running.')
def eat(self):
print(self.name + ' is eating.')
class Student(Person):
def __init__(self,name,age,classroom):
super().__init__(name,age)
self.classroom = classroom
class Teacher(Person):
pass
s = Student('Sam', 26, 'Python基础班')
print(s.classroom) # Python基础班
s.run()
'''
Sam is running.
'''
本文介绍了Python中文件的基本操作方法,包括文本文件的写入、读取及二进制文件的处理方式,并探讨了如何通过super调用父类的方法。

8490

被折叠的 条评论
为什么被折叠?



