zoukankan      html  css  js  c++  java
  • 文件基本操作

    f = open(r"d:/test.txt",'a')#类似于append追加,不修改原来的数据,新增内容,但是不能读
    data = open(r"d:/test.txt",'w')#写 ,不能读文件
    f.write(' hello')
    f.read()
    print(f)
    f.close() #关闭文件
    data.close() #关闭文件

    #读,写,追加,修改
    f = open('d:/test.txt','a') #追加
    f1 = open('d:/test.txt','r+') #r+读写
    f2 = open('d:/test.txt','w+') #w+写读
    #例子
    f2.write("----------d----------")
    print(f2.readlines())
    f3 = open('d:/test.txt','a+') #a+追加读
    f4 = open('d:/test.txt','wb') #二进制文件,会删除原来的数据
    #二进制写
    f4.write("hello binary ".encode())
    f4.close()
    #只打印前五行
    for i in range(5): #打印前五行
    print(f.readlines())
    for line in f.readlines(): #循坏,去空格
    print(line.strip())
    #不打印第10行(方式一)
    for index,line in enumerate(f.readlines()):
    if index ==9:
    print("------------")
    continue
    print(line.strip())
    #不打印第10行(方式二)
    # 如果很大文件,循坏读取数据,打印一行存一行,类似迭代器
    count = 0
    for line in f:
    if count ==9:
    continue
    count+=1
    print("--->",line)
    count +=1

    print(f.tell()) #把现在文件句柄指针位置的打印

    f.seek(0) #查找,返回的位置,0是返回第一行

    print(f.encoding)#编码

    print(f.fileno())#编号

    print(f.flush()) #刷新,缓存满才写入硬盘,不会写一条存一条
    #例子
    f1=open('d:/test.txt','w')
    f1.write("hello ")
    print(f.flush()) #如果没有这个一栏,则写不进硬盘

    print("ag:",dir(f.buffer))

    import sys
    import time
    sys.stdout.write() #标准输出
    sys.stdin.write() #标准输入

    #进度条
    for i in range(50):
    sys.stdout.write("#")
    sys.stdout.flush()
    time.sleep(0.1)

    f.truncate(5) #从5位置开始截断
  • 相关阅读:
    javascript学习一
    对软件工程课程的认识
    人月神话读后感
    项目开发总结报告(GB8567——88)
    MFC双缓冲绘图
    QT连接MySQL
    [QT学习]拷贝文件
    Arduino入门笔记【1】
    《人月神话》读后感以及软件工程总结
    十天冲刺任务(第二次冲刺)
  • 原文地址:https://www.cnblogs.com/mygodswangzi/p/11840892.html
Copyright © 2011-2022 走看看