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

    #文件操作 open()
    #open("路径 + 文件名",”读写模式")
    f=open('filepath','w')
     
    #读写模式:
    # r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件
    #常用读写模式
    #如:'rb','wb','r+b'等等
    #读写模式的类型有:
    #rU 或 Ua 以读方式打开, 同时提供通用换行符支持 (PEP 278)
    #w      以写方式打开,
    #a      以追加模式打开 (从 EOF 开始, 必要时创建新文件)
    #r+     以读写模式打开
    #w+     以读写模式打开
    #a+     以读写模式打开
    #rb     以二进制读模式打开
    #wb     以二进制写模式打开
    #ab     以二进制追加模式打开
    #rb+    以二进制读写模式打开
    #wb+    以二进制读写模式打开
    #ab+    以二进制读写模式打开
    #W      若文件存在,首先要清空,然后重新创建文件
    #a      把所有的数据追加到文件的尾部,即使seek指在其他的位置,如果文件不存在,则重新创建
     
    f.read([size])
    #size未指定则返回整个文件,如果文件大小>2倍内存则有问题.f.read()读到文件尾时返回""(空字串)
     
    file.readline()
    #返回一行
     
    file.readline([size])
    #返回包含size行的列表,size 未指定则返回全部行
     
    for line in f: print line
    #通过迭代器访问
     
    f.write("hello
    ")
    #如果要写入字符串以外的数据,先将他转换为字符串.
     
    f.tell()
    #返回一个整数,表示当前文件指针的位置(就是到文件头的比特数).
     
    f.seek(偏移量,[起始位置])
     
    #用来移动文件指针。偏移量:单位:比特,可正可负
    #起始位置:0-文件头,默认值;1-当前位置;2-文件尾
     
    f.close()
    #关闭文件
     
    f = open("c:\temp.txt","r+")   #可读可写模式
    f.write("123")                  #写入字符串
     
    f = open("c:\temp.txt","r")
    lines = f.readlines()           #读取全部内容
    for line in lines
    print line

    练习

    #coding=utf-8
    def write_demo ():
    f = open("123.dat",'w')
    f.write("hello python hello java hello python hello java hello python hello java hello python hello java hello python hello java hello python hello java ")
    f.close()
    def read_demo():
    f = open("123.dat",'r')
    cont = f.read(11111111)
    print(cont)
    print("---------")
    cont = f.read()
    print(cont)
    f.close()
    #write_demo()
    #read_demo()
    def readLines_demo():
    f = open("123.dat",'r')
    cont = f.readlines()
    print(type(cont))
    print(cont)

    #readLines_demo()

    def read_bigFile():
    f = open("123.dat",'r')
    cont = f.read(10)
    while len(cont) >0 :
    print(cont)
    cont = f.read(10)

    #read_bigFile()

    def read_demo2():
    f = open("123.dat",'r+')
    #print(f.read())
    f.write("aaasdf 1111111111111111111asdfasdfasdfasdfasdfasdfasdfadsfasdfasfd asdfasdfasdfasdfasdfasdf")
    print(f.read())
    f.close()

    #read_demo2()


    def copyFile():
    f1 = "123.dat"
    f2 = "123.dat.bak"
    #大文件复制
    fs1 = open(f1,'r')
    fs2 = open(f2,'w')
    cont1 = fs1.readline()

    while len(cont1)>0:
    #写入
    fs2.write(cont1)
    cont1 = fs1.readline()
    fs1.close()
    fs2.close()
    #copyFile()

    def read_random():
    f=open("123.dat",'r')
    str1 = f.read(3)
    print("read data is "+str1)
    position = f.tell()
    print("current position "+str(position))
    str1 = f.read(3)
    position = f.tell()
    print("current position "+str(position))
    f.close()
    #read_random()

    def read_seek():
    f=open("123.dat",'a+')
    f.seek(5,1)#从当前位置向前偏移5个字节
    f.read(3)
    print("current position "+str(f.tell()))
    f.seek(10,0)#从开始的位置向前都读取10个字节
    print(f.read(10))
    print("current position "+str(f.tell()))
    f.seek(10,2)#从结束的位置向开始的位置读取10个字节

    print(f.read(10))
    print("current position "+str(f.tell()))
    f.close()
    #read_seek()
    import os
    def file_rename():
    os.rename("123.dat.bak","123.bak")
    def file_del():
    os.remove("123.dat.bak1")
    #file_del()
    #copyFile()

    #批量重命名文件
    def rename_all_file():
    files = os.listdir("./")
    flag = "_[rename]_"
    for f in files:
    #print(type(f)) str
    pivot = f.find('.')
    if pivot < 0:
    continue
    filename = f[:pivot]
    suffix = f[pivot+1:]
    if filename.startswith("123") and filename.find(flag) < 0:
    os.rename(f,filename+flag+suffix)
    #rename_all_file()

    def copyFileByBinary(f1,f2):
    #大文件复制
    fs1 = open(f1,'rb')
    fs2 = open(f2,'wb')
    cont1 = fs1.read(1024)
    while len(cont1)>0:
    #写入
    fs2.write(cont1)
    cont1 = fs1.read(1024)
    fs1.close()
    fs2.close()
    #
    def copy_and_rename():
    basedir= "C:/Users/lq/Pictures/tset/"
    first_file = "wy0.jpg"
    for i in range(1,12):
    copyFileByBinary(basedir+first_file,basedir+str(i)+".jpg")
    #copy_and_rename()

    def file_mkdir():
    #os.mkdir("./testdir")
    #os.mkdir("./testdir2/inner")
    print(os.getcwd())
    #file_mkdir()
    def join(arr,join_falg):
    res = ""
    for a in arr:
    res += a+join_falg
    return res
    #递归创建文件夹
    def file_mkdir2(file):
    dirs = file.split("/")
    for i in range(1,len(dirs)+1):
    d = join(dirs[:i],"/")
    if not os.path.exists(d):
    os.mkdir(d)


    def get_file_parats(file):
    path_arr = file.split("/")
    last_index = file.rindex("/")
    parents_path = file[:last_index]
    return parents_path

    #删除所有叶子节点的文件
    def rm_dirs(path):
    if os.path.isfile(path):
    os.remove(path)
    elif len(os.listdir(path))==0:
    os.rmdir(path)
    else:
    dirs = os.listdir(path)
    for d in dirs:
    print(path+"/"+d)
    if os.path.isfile(path+"/"+d):
    os.remove(path+"/"+d)
    elif len(os.listdir(path+"/"+d))==0:
    os.rmdir(path+"/"+d)
    else:
    rm_dirs(path+"/"+d)
    rm_dirs("./testdir5/inner")
  • 相关阅读:
    mysql查询字段取前3位,后3位,中间3位,去除前3位,去除后3位
    10月份四季度
    JavaScript箭头函数的立即执行函数实现三元表达式执行多条语句
    JavaScript判断是否是同一天
    项目经理:是兄弟就一起加班吧
    技术人员转型项目经理的角色转换
    项目经理入职后,如何快速管理项目
    如何解决项目成员之间的冲突?
    提高各方面沟通效率,是项目经理该去做的事
    项目计划太复杂?试试思维导图
  • 原文地址:https://www.cnblogs.com/rocky-AGE-24/p/7638152.html
Copyright © 2011-2022 走看看