文件备份
'''
备份文件的步骤
1、选择需要备份的文件
2、读取备份文件
3、给新文件命名
4、写入新文件
'''
# 1、选择备份文件
# old_name = input("请输入需要备份的文件:")
old_name = "1.txt"
print(old_name, type(old_name))
# 查找指定字符的位置
index = old_name.rfind(".")
print(index)
# 把1.txt --> 1_备份. 生成一个新的文件名
new_name = old_name[:index] + "_备份" + old_name[index:]
print(new_name)
# 打开文件 注意使用的模式
old_f = open(old_name, "rb")
new_f = open(new_name, "wb")
# 永真循环读取 不停地读取内容
while True:
# 每次读取1024个字符
content = old_f.read(1024)
# 读到没有更多的数据的时候 终止循环
if len(content) == 0:
break
# 把内容写入新文件
new_f.write(content)
# 关闭文件
old_f.close()
new_f.close()
文件和文件夹操作
# 导入系统模块 这个模块有很多的函数可以进行文件和文件夹操作
import os
# 修改文件名
# os.rename("hello.txt", "2_hello.txt")
# 删除文件
# os.remove("test.txt")
# 创建文件夹
# os.mkdir("new_dir")
# 删除文件夹
# os.rmdir("new_dir")
# 打印当前的python解释器的路径
print(os.path)
# 获取指定目录的列表(文件和文件夹组成) 路径必须是英文
print(os.listdir("C:\Program Files\JetBrains\PyCharm Community Edition 2021.3.2"))
'''
1、备份1.txt文件,并且第三行加入 my name is bobi
2、将之前的学生管理系统 改写成文件模式
student_list = [
{"name": "麻", "age": 20, "gender": "man"},
{"name": "腾", "age": 21, "gender": "woman"},
{"name": "田", "age": 19, "gender": "man"}
]
列表放在文件中,然后对文件进行增删改查
'''
f = open("1.txt", "r")
con_list = f.readlines()
print(con_list)
con_list.insert(2, "my name is bobi")
f.close()
print(con_list)
随堂练习
# 1、备份1.txt文件,并且第三行加入 my name is bobi
# 备份函数
def back_up(lang):
# 打开旧文件
old_f = open("1.txt", "r")
# 将文件内容全部读取出来 形成一个列表
con_list = old_f.readlines()
# print(con_list)
# 在指定位置插入 内容
con_list.insert(2, lang)
# print(con_list)
# 打开新文件
new_f = open("1_备份.txt", "w")
# 往新文件中 添加数据
for i in con_list:
new_f.write(i)
# 关闭文件
old_f.close()
new_f.close()
if __name__ == '__main__':
# 函数调用
back_up("my name is bobi\n")
定义一个类
# 定义一个类 class: 类名()
# 类名后面可以不加括号
# Washer 必须是大驼峰
class Washer:
# 方法 方法就是类的功能或者行为 他可以实现的事情
def wash(self):
# self = haier = 类创建的对象 (不是唯一的)
# self, 谁调用wash 谁就是self
print("self:", self)
print("洗衣机开始洗衣服")
if __name__ == '__main__':
# 必须通过类 创建一个对象 类名() 就是创建对象
haier = Washer()
# 类对象,类的对象,实例化对象
print("haier:", haier, type(haier))
# 可以通过类的对象 调用类中的方法
# 使用的对象 会传递给self
haier.wash()
# 创建另外一个对象
meidi = Washer()
print("meidi", meidi)
meidi.wash()
类外面添加属性
# 定义一个类
class Washer:
# 方法 方法就是类的功能或者行为 他可以实现的事情
def wash(self):
# self = haier
print("洗衣机开始洗衣服")
# 在方法中打印类对象的属性
print(self.height)
print(self.weight)
if __name__ == '__main__':
# 必须通过类 创建一个对象 类名() 就是创建对象
haier = Washer()
# 类对象,类的对象,实例化对象
print("haier:", haier, type(haier))
# 给类的对象添加属性
haier.height = 150
haier.weight = 60
# 类的对象调用方法
haier.wash()
'''
随堂练习:
1、定义一个自行车类
自行车有一个方法 ride 方法中可以打印一些东西
创建一个凤凰牌自行车,
给凤凰牌自行车添加属性 颜色是白色,age 25,
在ride方法中打印 类对象的属性
'''
练习
'''
随堂练习:
1、定义一个自行车类
自行车有一个方法 ride 方法中可以打印一些东西
创建一个凤凰牌自行车对象,
给凤凰牌自行车添加属性 颜色是白色,age 25,
在ride方法中打印 类对象的属性
'''
# 定义一个类
class Baike():
# 定义ride方法
def ride(self):
print("我骑自行车 车速180")
# self = phoenix
print(self.color)
print(self.price)
if __name__ == '__main__':
# 创建一个凤凰牌自行车对象
phoenix = Baike()
# 给凤凰牌自行车 添加属性
phoenix.color = "white"
phoenix.price = 2000
# 通过类的对象调用类中的方法
phoenix.ride()
在类里面定义属性
# 定义一个类
class Baike():
# 定义ride方法
def ride(self):
print("我骑自行车 车速180")
# self = phoenix
# 给类对象 添加对象属性
self.color = "black"
self.price = 250
print(self.color)
print(self.price)
if __name__ == '__main__':
# 创建一个凤凰牌自行车对象
phoenix = Baike()
# 通过类的对象调用类中的方法
phoenix.ride()
int魔术方法
# 定义一个人类
class Person():
def __init__(self):
'''
init魔术方法 在实例化对象的时候被触发
self = lyg
'''
print("init被触发")
# 定义对象属性
self.name = "罗"
self.age = 20
# 定义一个普通方法
def eat(self):
print(f"{self.name}在吃饭, 他今年{self.age}岁")
if __name__ == '__main__':
# 实例化罗一根 对象
lyg = Person()
# 调用类中的方法
lyg.eat()
类传递参数
# 定义一个类
class Person():
def __init__(self, name, age):
'''
init魔术方法 在实例化对象的时候被触发
self = 类的对象
'''
print("init被触发")
# 定义对象属性
self.name = name
self.age = age
# 定义一个普通方法
def eat(self):
print(f"{self.name}在吃饭, 他今年{self.age}岁")
if __name__ == '__main__':
# 实例化罗一根 对象
lyg = Person("罗", 20)
# 调用类中的方法
lyg.eat()
lh = Person("吕", 18)
lh.eat()
测试
f = open("1.txt", "r")
con_list = f.readlines()
print(con_list)
con_list.insert(2, "my name is bobi")
f.close()
print(con_list)
str魔术方法
# 定义一个Monkey类
class Monkey:
def __init__(self, name, hair):
'''
name = "金丝猴"
hair = “gold”
'''
self.name = name
self.hair = hair
def __str__(self):
'''
当实例化对象被打印的时候 触发 打印输出的是返回值
'''
return f"这之猴子的名字叫{self.name}, 毛发颜色是{self.hair}"
if __name__ == '__main__':
# 实例化一个金丝猴对象
jsh = Monkey("金丝猴", "gold")
print(jsh)
del魔术方法
# 定义一个类
class Computer:
def __init__(self, name, address):
self.name = name
self.address = address
def __del__(self):
'''
触发时机:在对象被删除的时候 触发
'''
print(f"{self.name}对象被删除")
if __name__ == '__main__':
# 定义一个联想的电脑
Lenovo = Computer("联想", "青岛")
# 删除对象
del Lenovo
继承
# 定义一个父类
# Python中所有的类都默认继承object基类
class A(object):
def __init__(self):
self.num = 1
def print_info(self):
print(self.num)
# 定义一个子类
# 在B后面的括号中写A说明 A是B的父类
class B(A):
pass
if __name__ == '__main__':
# 实例化一个B的对象
b = B()
# b调用父类的方法
b.print_info()
# 子类的对象调用父类的属性
print(b.num)
单继承
# 定义一个师傅类
class Master(object):
def __init__(self):
self.kongfu = '[大师煎饼果子配方]'
def make_cake(self):
print(f"运用{self.kongfu}制作煎饼果子")
# 定义一个徒弟类 只有一对父子关系 的叫做单继承
class Prentice(Master):
pass
if __name__ == '__main__':
# 创建一个徒弟类的对象
rpp = Prentice()
# 使用徒弟类的对象去 使用kongfu
print(rpp.kongfu)
# 使用徒弟类的对象去调用 大师类的方法
rpp.make_cake()
多继承
# 定义一个大师类
class Master(object):
def __init__(self):
self.kongfu = '[大师煎饼果子配方]'
def make_cake(self):
print(f"运用{self.kongfu}制作煎饼果子")
# 定义一个学校类
class School(object):
def __init__(self):
self.kongfu = "[连锁品牌的煎饼果子配方]"
def make_cake(self):
print(f"运用{self.kongfu}制作煎饼果子")
# 定义一个徒弟类 一个类有两个 甚至多个父类 叫做多继承
# 寻找父类的方法 会找第一个父类
class Prentice(School, Master):
pass
if __name__ == '__main__':
# 创建一个徒弟类的对象
rpp = Prentice()
# 使用徒弟类的对象去 使用kongfu
print(rpp.kongfu)
# 使用徒弟类的对象去调用 大师类的方法
rpp.make_cake()
子类重写父类的属性和方法
# 定义一个大师类
class Master(object):
def __init__(self):
self.kongfu = '[大师煎饼果子配方]'
def make_cake(self):
print(f"运用{self.kongfu}制作煎饼果子")
# 定义一个学校类
class School(object):
def __init__(self):
self.kongfu = "[连锁品牌的煎饼果子配方]"
def make_cake(self):
print(f"运用{self.kongfu}制作煎饼果子")
# 定义一个徒弟类 一个类有两个 甚至多个父类 叫做多继承
# 寻找父类的方法 会找第一个父类(超类)
class Prentice(School, Master):
# 定义子类自己的init函数 不会触发父类的init
def __init__(self):
self.kongfu = "[独创煎饼果子配方]"
# 重写父类的方法
def make_cake(self):
print(f"运用{self.kongfu}制作煎饼果子")
if __name__ == '__main__':
# 创建一个徒弟类的对象
rpp = Prentice()
# 使用的是子类自己的配方
print(rpp.kongfu)
# 使用的是子类自己的方法
rpp.make_cake()