• 文件操作


    # 文件操作:文件上传,保存log
    # 系统函数:open(file,mooe)
    # mode : r w rb wb r:read w:write b:binary 二进制 字节 r w:纯文本文件 rb,wb:文本,图片,音乐,影片,
    # 读操作:open(path/filename,'rt')-------->返回值: stream(管道)
    # streanm.read()---------->读取管道中内容
    # 注意:如果传递的path/或者filename 错误 就会报出错误


    # stram = open(r'C:UsersAdministratorDesktopaa.txt')
    # a = stram.read()
    # print(a)

    # result = stram.readable()
    # print(result) #判断能不能读取返回的是一个布尔值

    # while True:
    # resul = stram.readline() #如果已经读过一遍了,那么就读不出了。相当于已经管道中已经搬运过一次了,只表示读取一行
    # print(resul) #读过来的东西会默认加入一个换行
    # if resul: #如果要全部读取就要加入循环。
    # break

    # resul1 = stram.readlines() #输出的结果是一个['hello world'],列表的形式。
    # print(resul1)
    # for i in resul1:
    # print(i) #取出列表中的东西


    # 读取图片 aa = open(r' Epythonpicturepractice.jpg','rb') #:[Errno 2] No such file or directory: '
    # #E\python\picture\practice.jpg' result = aa.read() print(aa)

    # 写文件
    # stream = open(r'C:UsersAdministratorDesktopaa.txt', 'w')
    # s = '''
    # zzh,zzh,zzh
    # zzh
    # ,zach
    # '''

    # a = stream.write(s)
    # print(a)
    # stream.close()#释放你的管道资源
    # stream.write('zzh') # 释放管道之后就会报错,而且会覆盖原来的东西 每一次进行写操作的时候都会先进行一个清空的操作
    # stream.writelines(['zzh111 ', 'syn ', 'zzzz ']) # writelines(iterable) 是一个可迭代的,要有换行的效果必须自己去进行添加

    # 如果mode是a的模式,那么就会有一个追加的效果,就不会进行清空。这样的话写东西就不会清空之前的内容
    # 可以看出open是一个建立流的过程


    # 文件的复制
    # 原文件=C:UsersAdministratorDesktopaa.txt
    # 目标的=C:UsersAdministratorDesktopaa1.txt
    # 那么需要建立两个通道,一个读,一个写 。读原文件,写目标的文件

    # with open(r'C:UsersAdministratorDesktopaa.txt', 'r') as a: # 这样写可以让它自动释放资源
    # a1 = a.read() # 读取文件的内容
    # with open(r'C:UsersAdministratorDesktopaa1.txt', 'w') as b:
    # b1 = b.write(a1)
    # print('文件复制完成') # 输出:文件复制完成


    # 批量复制 需要一个os模块
    # os模块的中文手册:https://www.cnblogs.com/wsk312138147/p/9338459.html
    # import os
    #
    # print(os.path.dirname(__file__)) # 输出当前文件的路径 :E:/python
    #
    # with open(r'C:UsersAdministratorDesktopaa.txt', 'r') as a: #正常的读
    # a1 = a.read() # 返回值找一个东西接
    # path = os.path.dirname(__file__) #找到目前的绝对路径
    # path1 = os.path.join(path,'aaa.txt') #在绝对路径下建立一个文件
    # with open(path1, 'w') as bb: #并打开一个通道 方向是写入的方向
    # bb.write(a1) #把a1写入


    # isabs = 绝对路径 = 要写完整的路径 i= is 用于判断的 返回值是一个布尔类型的
    # 相对路径是 r'../picture/practice.jpg 同级别的可以直接进,不用加.. 不同级别的需要加上.. 一级加一个.. 可以加多个'
    # import os
    # os.path
    # os.path.isabs(r'C:UsersAdministratorDesktopaa.txt')、


    # 如何得到绝对路径
    # path = os.path.dirname(__file__) 得到当前文件夹的路径
    # print(path)
    # dirname = directory 目录的意思
    # os.path.abspath 通过相当路径得到一个绝对路径

    # 得到当前文件的一个绝对路径,可以使用这一种方法
    # os.path.abspath(__file__)
    # os.getcwd() 得到当前的文件夹的绝对路径


    # path = r'c/c/c/c/c/c//........'
    # os.path.split(path) 返回的是一个元组。前面是路径,后面是一个文件名


    '''
    os的常用的模块
    join 进入当前的一个路径
    dirname 得到当天的一个
    os.path.abspath(relative_path)
    返回绝对路径

    os.path.basename(path)
    返回文件名

    os.path.dirname(path)
    返回目录名,不包含文件名。注意:返回的路径名不含最后的斜杠

    os.path.split(path)
    将 path 分解成 (路径, 文件名)

    os.path.join(path1[, path2[, …]])
    合并多个路径

    os.path.normpath(path)
    将路径正规化:去除多余的分隔符,将 . 和 … 变成真实路径,处理错误的斜杠

    os.path.exists(path)
    文件或路径是否存在并且有权限访问

    os.path.isabs(path), isfile(path), isdir(path), islink(path)
    isabs: 是否绝对路径
    isfile: 是否文件
    isdir: 是否路径
    islink: 是否link

    os.path.walk(path, callback, arg)
    遍历路径,对路径中的每个文件调用callback函数

    callback函数的原型如下:
    callback(arg, path, files)
    @arg: walk函数的参数
    @path: 路径
    @files: 路径下的所有文件

    '''

    # dir = os.getcwd()
    # print(dir) 这样得到的是一个当前文件的路径

    # os.listdir(r'c/p1/path') 返回目录下 所有文件夹的名字和文件的名称 保存到了一个列表中了

    # 如何创建一个文件夹 (代码的方法)
    # os.mkdir(r'c:p3') 文件夹如果已经存在的话就会报错

    # 删除一个文件夹 (代码的方法) 只能删空文件夹,有的话就会报错
    # os.rmdir(r'c:p3')

    #删除不是空的文件夹
    #os.removedirs(r'c:/p3') 删除多个目录,就是p3下面还有多个文件夹,就不可以删除

    #要删文件
    #os.rmove(r'c:/p3/aaa.txt) 是删除文件的

    #如何删除一个有文件在里面的文件夹
    # filelist = os.listdir(path)
    # for file in filelist:
    # path1 = path.os.join(path,file)
    # os.rmove(path1)
    #else:
    # os.rmdir(path)
    # print('删除成功')

    #切换目录
    # os.chdir(r'c:/p1')
    # os.getcwd() 换到这个c:/p1的路径下




    #练习 文件的批量复制
    import os
    src_path = r'C:UsersAdministratorDesktop111'
    target_path = r'F:222'

    def copy (src,target):
    if os.path.isdir(src) and os.path.isdir(target):
    filelist = os.listdir(src)
    for file in filelist:
    path = os.path.join(src,file)
    path1 = os.path.join(target)
    with open(path) as first :
    dier = first.read()
    with open(path1) as disan:
    disan.write(dier)

    else:
    print('复制完毕')




    import os

    aa = r'c:UsersAdministratorDesktop111'
    bb = r'c:UsersAdministratorDesktop222'


    def copy(src, target):
    if os.path.isdir(src) and os.path.isdir(target):
    filelist = os.listdir(src)
    for file in filelist:
    path = os.path.join(src, file)
    path1 = os.path.join(target)
    with open(path,'r') as first:
    dier = first.read()
    with open(path1,'w') as disan:
    disan.write(dier)
    print('wancheng')


    copy(aa,bb)
    报错 需要后期再看一看
    Traceback (most recent call last):
    File "E:/untitled/代码测试.py", line 20, in
    copy(aa,bb)
    File "E:/untitled/代码测试.py", line 15, in copy
    with open(path1,'w') as disan:
    PermissionError: [Errno 13] Permission denied: 'c:UsersAdministratorDesktop222'
  • 相关阅读:
    废水回收
    XJOI网上同步训练DAY6 T2
    XJOI网上同步训练DAY6 T1
    Codeforces 351B Jeff and Furik
    对拍 For Linux
    Codeforces 432D Prefixes and Suffixes
    Codeforces 479E Riding in a Lift
    Codeforces 455B A Lot of Games
    Codeforces 148D Bag of mice
    Codeforces 219D Choosing Capital for Treeland
  • 原文地址:https://www.cnblogs.com/SmartCat994/p/12306893.html
Copyright © 2020-2023  润新知