当前位置: 移动技术网 > IT编程>脚本编程>Python > 5、文件处理

5、文件处理

2020年04月09日  | 移动技术网IT编程  | 我要评论

美容方法网,甘肃省招标中心,168电影

1.深浅拷贝

  • 拷贝模块

    import copy
    
  • 不可变类型(元组除外)拷贝后内存地址相同

  • 可变类型,拷贝后会新生成一个内存地址

  • 浅拷贝

    • 只拷贝整个数据类型的表面内存地址

      • 无数据类型嵌套
      #不可变类型
      v1 = 'sssdddw'
      v2 = copy.copy(v1)   #引用模块
      print(id(v1),id(v2))	#2032429695216 2032429695216
      
      #可变类型,再开辟一块新内存地址进行存储
      v1 = [11,2,3]
      v2 = copy.copy(v1)
      print(id(v1),id(v2))	#1782467694976 1782467643392
      
      • 有数据类型嵌套
      #第一层可变类型数据的内存地址被重新创建,里层的内存地址则没变
      v1 = [1,2,3,[1,2,3]]
      v2 = copy.copy(v1)
      print(id(v1),id(v2))	#2510241515712 2510241705472
      print(id(v1[3]),id(v2[3]))	#2510241567296 2510241567296
      
  • 深拷贝

    • 不管数据类型有几层都重新创建内存地址存储

      import copy
      v1 = [1,2,3,[1,2,3]]
      v2 = copy.deepcopy(v1)
      print(id(v1),id(v2))	#1391916533888 1391916723584
      print(id(v1[3]),id(v2[3]))	#1391916585408 1391916722944
      
  • 特殊性

    • 元组

      • 如果元组只有一层那么深浅拷贝内存地址都不变

        import copy
        v1 = (1,2,3)
        v2 = copy.copy(v1)
        print(id(v1),id(v2))	#1993940503296 1993940503296
        
        import copy
        v1 = (1,2,3)
        v2 = copy.deepcopy(v1)
        print(id(v1),id(v2))	#2576028699392 2576028699392
        
      • 如果元组中嵌套列表在深拷贝中则会重新开辟内存地址

        import copy
        v1 = (1,2,3,[11,22,33])
        v2 = copy.deepcopy(v1)
        print(id(v1),id(v2))
        
  • 示例

    #1
    import copy
    v1 = '123'
    v2 = copy.copy(v1)
    print(v1 == v2)	#true
    print(v1 is v2)	#true
    v3 = copy.deepcopy(v1)
    print(v1 == v3)	#true
    print(v1 is v3)	#true
    #2
    import copy
    v1 = [11,'222',222]
    v2 = copy.copy(v1)
    print(v1 == v2)	#true
    print(v1 is v2)	#false
    v3 = copy.deepcopy(v1)
    print(v1 == v3)	#true
    print(v1 is v3)	#false
    #3
    import copy
    v1 = [11,'222',222,[33,44,22]]
    v2 = copy.copy(v1)
    print(v1[3] == v2[3])	#true
    print(v1[3] is v2[3])	#true
    v3 = copy.deepcopy(v1)
    print(v1[3] == v3[3])	#true
    print(v1[3] is v3[3])	#false
    

2.文件操作

  • 打开文件

    file = open('22.txt',mode='r',encoding='utf-8')
         open(文件路径,打开方式,文件编码)
    
  • 读取和写入文件

    • r ,read,可读

      • 只能读不能写

        file = open('22.txt',mode='r',encoding='utf-8')
        data = file.read()
        print(data)	
        file.close()
        
    • w, write,可写

      • 打开文件的同时清空文件内容,再写入数据

      • 如果文件不存在则先创建再写入数据

      • 只能写字符串

        file = open('22222.txt',mode='w',encoding='utf-8')
        file.write('123')
        file.close()
        
    • a,append 追加

      • 追加数据到文件内部数据末尾

        file = open('22222.txt',mode='a',encoding='utf-8')
        file.write('123')
        file.close()
        
    • r+,可读可写

      • 可以通过seek调光标位置来指定写入和查看位置
      #文件先写在查看(清除原有内容)
      file = open('22222.txt',mode='r+',encoding='utf-8')
      file.write('123')
      data = file.read()
      print(data)	#123
      file.close()
      #指定位置写入(则会覆盖指定位置字符后面的数据)
      file = open('22222.txt',mode='r+',encoding='utf-8')
      file.seek(2)
      file.write('666')
      data = file.read()
      print(data)	#这样只能读取写入后光标存在位置之后的数据
      file.close()
      
      file = open('22222.txt',mode='r+',encoding='utf-8')
      file.seek(2)
      file.write('666')
      file.seek(0)
      data = file.read()
      print(data)	#这样可以读取全部数据
      file.close()
      #如果想从文件末尾去添加数据
      file = open('22222.txt',mode='a+',encoding='utf-8')
      date = file.read()
      file.write('好')
      file.seek(0)
      date = file.read()
      print(date)	#123456好
      file.close()
      
    • w+,可写可读

      • 打开文件的同时清空文件内容,再写入数据
      • 可以通过seek调光标位来读取
      file = open('22222.txt',mode='w+',encoding='utf-8')
      file.write('好')
      file.seek(0)
      date = file.read()
      print(date)
      file.close()
      
    • a+,追加读

      • 追加数据到文件内部数据末尾
      • 可以通过seek调光标位置来读取数据
      file = open('22222.txt',mode='a+',encoding='utf-8')
      file.write('好')
      file.seek(0)
      date = file.read()
      print(date)	#1dfdsfdf好
      file.close()
      
  • 关闭文件

    • 文件操作完毕,文件未关闭情况下文件内部数据会存到内存中,并未保存到硬盘
    file.close()
    
  • 练习

    #练习一、换行写入数据
    file = open('22222.txt',mode='w',encoding='utf-8')
    while true:
        read_input = input('请输入你要说的话(输入n结束):')
        if read_input.lower() == 'n':
            break
        file.write(read_input + '\n')
    file.close()
    
    file = open('22222.txt',mode='r',encoding='utf-8')
    date = file.read()
    print(date)
    file.close()
    
    #练习二、把下面列表的内容输入到文件中保存
    '''
    user = [
        {'name':'alex','pwd':'123'},
        {'name':'eric','pwd':'oldboy'}
    ]
    '''
    user = [
        {'name':'alex','pwd':'123'},
        {'name':'eric','pwd':'oldboy'}
    ]
    file = open('22222.txt',mode='w',encoding='utf-8')
    for i in user:
        line = "%s|%s\n"%(i['name'],i['pwd'])
        file.write(line)
    file.close()
    file = open('22222.txt',mode='r',encoding='utf-8')
    date = file.read()
    print(date)
    file.close()
    
    #练习三、把练习二产生的文件里面的数据,提取到列表中保存
    #解法一(适用于数据量少的情况下使用)
    file =open('22222.txt',mode='r',encoding='utf-8')
    date = file.read()
    date = date.strip()
    date = date.split('\n')
    print(date)
    file.close()
    #解法二
    v1 = []
    file =open('22222.txt',mode='r',encoding='utf-8')
    for line in file:
        line = line.strip()
        v1.append(line)
    print(v1)
    file.close()
    
    #练习四、编写代码实现账号密码存储及登入验证
    #创建密码保存到文件中
    file = open('账号密码.txt',mode='a',encoding='utf-8')
    while true:
        user = input('请输入用户名:')
        if user.lower() == 'n':
            break
        pwd = input('请输入密码:')
        file.write('%s|%s\n'%(user,pwd))
    file.close()
    
    #从文件中验证账号密码
    
    user_pwd = []
    file = open('账号密码.txt',mode='r',encoding='utf-8')
    for i in file:
        i = i.strip()
        user_pwd.append(i)
    file.close()
    state = 1
    user_1 = input('请输入登入用户名:')
    pwd_1 =input('请输入登入密码:')
    for item in user_pwd:
        item_1 = item.split('|')
        if user_1 == item_1[0] and pwd_1 == item_1[1]:
            break
        else:
            state = 0
            break
    if state:
        print('登入成功!!!')
    else:
        print('登入失败')
    

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网