zoukankan      html  css  js  c++  java
  • Day8 文件

    文件操作
    创建文件,不可读
    f = open(file='文件操作.txt',mode='w')
    f.write('qiangzi it 175 80 2000 xifeng it 185 70 4000 ')
    f.close()

    只读文件,不能写
    f = open(file='文件操作.txt',mode='r')
    # print(f.read())
    print(f.readline())
    data = f.read()
    print(data)
    f.close()

    添加文件,不能读
    f = open(file='文件操作.txt',mode='a')
    f.write('daqiang it 182 75 6000 loangzong boss 170 75 1000000 chao it 180 78 8000 sunqiang it 170 70 8000')
    f.close()

    #文件循环 for循环
    f = open(file='文件操作.txt',mode='r')
    for line in f:
    line = (line.split())
    height = int(line[2])
    weight = int(line[3])
    if height > 175 and weight > 75:
    print(line)



    file类的其他的必要功能
    #.seek  把光标移到哪个位置 3字节一中文
    f = open(file='文件操作.txt')
    f.seek(6)
    print(f.read())


    .flush 强制把缓存里的数据刷到硬盘里
    tell 返回光标的位置
    f = open(file='文件操作.txt')
    f.seek(6)
    print(f.read())
    print(f.tell())

    i it 175 80 2000
    xifeng it 185 70 4000
    daqiang it 182 75 6000
    loangzong boss 170 75 1000000
    chao it 180 78 8000
    sunqiang it 170 70 8000

    154

    truncate()  截取

    # f = open(file='文件操作.txt',mode='w')
    # f.write('qiangzi it 175 80 2000 xifeng it 185 70 4000 ')
    # f.close()
    #
    #
    # f = open(file='文件操作.txt',mode='r')
    # # print(f.read())
    # print(f.readline())
    # data = f.read()
    # print(data)
    # f.close()
    #
    #
    # f = open(file='文件操作.txt',mode='a')
    # f.write('daqiang it 182 75 6000 loangzong boss 170 75 1000000 chao it 180 78 8000 sunqiang it 170 70 8000')
    # f.close()

    #文件循环
    # f = open(file='文件操作.txt',mode='r')
    # for line in f:
    # line = (line.split())
    # height = int(line[2])
    # weight = int(line[3])
    # if height > 175 and weight > 75:
    # print(line)

    # #seek 把光标移到哪个位置 3字节一中文
    # f = open(file='文件操作.txt')
    # f.seek(6)
    # print(f.read())
    # print(f.tell())

    # import os
    # old_file = '文件操作1.txt'
    # new_file = '文件操作1.txt.new'
    # f = open(file=old_file,mode='r')
    # f_new = open(file=new_file,mode='w')
    # for line in f:
    # old_str = 'it'
    # new_str = 'big boss'
    # if 'it' in line:
    # line = line.replace(old_str,new_str)
    # else:
    # line = line
    # f_new.write(line)
    # f.close()
    # f_new.close()
    # os.replace('文件操作1.txt.new','文件操作1.txt')



    # import os
    # old_file = '文件操作.txt'
    # new_file = '文件操作.txt.new'
    # f = open(file=old_file,mode='r')
    # f_new = open(file=new_file,mode='w')
    # old_str = 'it'
    # new_str = 'big big boss'
    # for line in f:
    # if old_str in line:
    # line = line.replace(old_str,new_str)
    # f_new.write(line)
    # f.close()
    # f_new.close()
    # os.replace(new_file,old_file)





  • 相关阅读:
    极客技术专题【011期】:EasyUI初级教程
    帮助自定义选择框样式的Javascript
    利用HTML5与jQuery技术创建一个简单的自动表单完成
    30个iPhone健康应用帮助你保持身体健康
    如何构建下拉滑动式响应导航菜单
    推荐十款来自极客标签的超棒前端特效[第十三期]
    重新设计网站的10点建议
    创建CSS3警示框渐变动画
    17种新型的响应式开发框架
    使用jQuery创建模态窗口登陆效果
  • 原文地址:https://www.cnblogs.com/wzq1997/p/12945556.html
Copyright © 2011-2022 走看看