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位置开始截断
  • 相关阅读:
    join命令
    参与者模式
    字符串中的第一个唯一字符
    Git与SVN对比
    惰性模式
    .NET Conf 2020
    使用Github部署Azure应用服务
    Azure Terraform(一)入门简介
    打日志还能打出个线上Bug_ 太难了。。。
    让API并行调用变得如丝般顺滑的绝招
  • 原文地址:https://www.cnblogs.com/mygodswangzi/p/11840892.html
Copyright © 2011-2022 走看看