Day17 Python课程学习内容

本文介绍了Python中文件的基本操作方法,包括文本文件的写入、读取及二进制文件的处理方式,并探讨了如何通过super调用父类的方法。

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.
'''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值