J4ckey
J4ckey
发布于 2025-11-07 / 13 阅读
0

Python-11-7

创建文件同时读取文件

with open('./test.txt', mode='a', encoding='utf-8') as fp:
    fp.write('the test file\n')

with open('./test.txt', mode='r', encoding='utf-8') as fp:
    for line in fp:
        print(line)

循环创建100个文件

for i in range(1,100):
    with open(f'./test-{i}.txt', mode='a', encoding='utf-8') as fp:
        fp.write(f'the test file is {i}\n')

类的用法

class Employee:
    def __init__(self, name, account):
        self.name = name
        self.account = account
    def print_info(self):
        print(f'姓名:{self.name},工号:{self.account}')
    def calculate_monthly_pay(self):
        pass

class FulltimeEmployee(Employee):
    def __init__(self, name, account, monthly_salary):
        super().__init__(name, account)
        self.monthly_salary = monthly_salary
    def calculate_monthly_pay(self):
        print(f'月薪为{self.monthly_salary}')

class ParttimeEmployee(Employee):
    def __init__(self, name, account, daily_salary, work_days):
        super().__init__(name, account)
        self.daily_salary = daily_salary
        self.work_days = work_days
    def calculate_monthly_pay(self):
        print(f'月薪为{self.daily_salary * self.work_days}')

part = ParttimeEmployee('part','0-1', 100, 3)
part.print_info()
part.calculate_monthly_pay()

full = FulltimeEmployee('full', '0-2', 3000)
full.print_info()
full.calculate_monthly_pay()