qfxx第三周4作业

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/7/25 0025 9:17
# @Author  : joker-syc
# @Site    : 
# @File    : test.py
# @Software: PyCharm

#copy.deepcopy()  深拷贝可以拷贝类型!!!

class Test:

    def __new__(cls, *args, **kwargs):
        print("dd")

    def __init__(self):
        print("ss")

test=Test()

class Singleton(object):
    __instance = None

    def __new__(cls, *args, **kwargs):
        if cls.__instance is None:
            cls.__instance = super().__new__(cls)
        return cls.__instance

    def __init__(self, status_number):
        self.status_number = status_number


singl = Singleton("123")
singl2 = Singleton("122")
print(singl)
print(singl2)
print(singl.status_number)
print(singl2.status_number)
print(singl is singl2)


'''
with open(file,mode,encoding,...) as f :
    f.read()
file:文件位置
mode:读取模式   rb 以二进制读取
encoding:读取的编码格式
'''

def readAndWrite(nun):
    with open(file='test.txt',mode='a',encoding='utf-8') as f:
        for a in range(nun):
            f.write('shadiao')

readAndWrite(3)

import os
def readAndWrite(path):
    dirlist=os.listdir(path)
    print(dirlist)
    filelist=[]
    for a in dirlist:
        filelist.append(os.path.join(path, a))
        if os.path.isdir(os.path.join(path,a)):
            for dd in os.listdir(os.path.join(path,a)):
                filelist.append(os.path.join(path,a)+"\\"+dd)
    # print(filelist)
    c=0
    for b in filelist:
        # print(b)
        if os.path.exists(os.path.join(path,"target")):
            pass
        else:
            os.mkdir(os.path.join(path,"target"))
        if os.path.isdir(b):
            if os.path.exists(os.path.join(path,"target")+'\\'+dirlist[c]):
                pass
            else:
                os.mkdir(os.path.join(path,"target")+'\\'+dirlist[c])
            c+=1
            continue
        # print(dirlist[c])
        with open(file=os.path.join(path,"target")+'\\'+dirlist[c],mode='w',encoding='utf-8') as f:
            with open(file=filelist[c],mode='r',encoding='utf-8') as  e:
                # print(filelist[c])
                f.write(e.read())
                c+=1

readAndWrite('C:\\Users\\Administrator\\PycharmProjects\\workhard\\venv\\Include\\day14')

'''
1.将字典写入文件,读出字典
'''
def writeFile(file):
    path=r'C:\Users\Administrator\PycharmProjects\workhard\venv\Include\day14\test.txt'
    with open(file=path,mode='r',encoding='utf-8') as f:
        # f.write(str(file))
        print(f.read())

    # with open(file=path,mode='w',encoding='utf-8') as d:
    #     print(d.read())

writeFile({1:2,3:4})

'''
2.将对象写入文件,读出对象
'''
class Person:

    def __init__(self,name,sex,phonenumber):
        self.name=name
        self.sex=sex
        self.phonenumber=phonenumber

    def person2dict(self):
        return {"name":self.name,"sex":self.sex,"phonenumber":self.phonenumber}

    @staticmethod
    def dict2person(dict1):
        person=Person(dict1['name'],dict1['sex'],dict1['phonenumber'])
        return person

if __name__ == '__main__':
    person=Person("沙雕",'男','123455')
    print(person.person2dict())
    print(type(person.person2dict()))
    dict1=person.person2dict()
    print(Person.dict2person(dict1))
    print(type(Person.dict2person(dict1)))
    print(Person.dict2person(dict1).name)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值