• python:创建文件夹:写入文本1:读取txt:读取Excel文件遍历文件夹:


    https://blog.csdn.net/u011956147/article/details/80369731

    创建文件夹:

    import os
    import shutil

    def buildfile(echkeyfile):
        if os.path.exists(echkeyfile):
                #创建前先判断是否存在文件夹,if存在则删除
                shutil.rmtree(echkeyfile)
                os.makedirs(echkeyfile)
            else:
                os.makedirs(echkeyfile)#else则创建语句
        return echkeyfile#返回创建路径
    #传入的参数是需要创建文件夹的路径,比如我想在D盘下创建一个名字为newfile的文件夹,则传入参数为r’ D: ewfile’。同样,返回的参数也是r’ D: ewfile’

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12

    写入文本1:

    import codecs
    def write_txt(txt, path):
        f = codecs.open(path, 'a', 'utf8')
        f.write(str(txt))
        f.close()
    # 传入参数为txt,path;txt为需要写入的内容,数据类型为字符串,path为写入的内容,数据类型为字符串。
    # 传入的path需如下定义:path= r’ D: ext.txt’
    # f = codecs.open(path, 'a', 'utf8')中,codecs为包,需要用impor引入,’a’表示追加写入txt,可以换成’w’,表示覆盖写入。'utf8'表述写入的编码,可以换成'utf16'等。

        1
        2
        3
        4
        5
        6
        7
        8

    写入文本2(等同于写入文本1,但是这个比较常用):

    import codecs
    def writetxt(path, content, code):
        with codecs.open(path, 'a', encoding=code)as f:
            f.write(content)
        return path+' is ok!'

        1
        2
        3
        4
        5
        6

    读取txt:

    def read_txt(path):
        with open(path, 'r', encoding='utf8') as f:
            lines = f.readlines()
        return lines
    # 表示按行读取txt文件,utf8表示读取编码为utf8的文件,可以根据需求改成utf16,或者GBK等。
    # 返回的为数组,每一个数组的元素代表一行,若想返回字符串格式,可以将改写成return ‘ ’.join(lines)

        1
        2
        3
        4
        5
        6
        7

    读取Excel文件:

    import xlrd
    def read_xls(path):
        xl = xlrd.open_workbook(path)
        sheet = xl.sheets()[0] # 0表示读取第一个工作表sheet
        data = []
        for i in range(0, sheet.ncols): # ncols表示按列读取
            data.append(list(sheet.col_values(i)))
        return data
    # xlrd为第三方包,可以通过用pip下载,具体操作:打开运行,输入cmd→在cmd中输入pip install xlrd,enter →等待安装完成即可。在后续若存在需要使用的第三方包,都可以通过这种方式下载和安装。
    # 传入参数为path,path为excel所在路径。
    # 传入的path需如下定义:path= r’ D:excel.xlsx’或path= r’ D:excel.xls’
    # col_values(i)表示按照一列中的所有单元格遍历读取
    # 可以根据需求,把col替换成row,则表示按行读取
    # return data :返回的data是一个二维数组,根据col和row,传回的数据呈现形式也不同,即row是col的转置。

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15

    遍历文件夹:

    def file_walker(path):
        fileArray = []
        for root, dirs, files in os.walk(path):
            for fn in files:
            eachpath = str(root+'\'+fn)
            fileArray.append(eachpath)
        return fileArray
    # 传入参数为path,path为需要遍历的文件夹路径。
    # return fileArray 返回的是当前文件下所有文件的绝对路径
    ---------------------  
    作者:FelixFuu  
    来源:CSDN  
    原文:https://blog.csdn.net/u011956147/article/details/80369731  
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    ecshop编辑器fckeditor换百度ueditor编辑器教程
    让ecshop用户登录评价以可择匿名评价
    shell与if相关参数
    Spring 框架的设计理念与设计模式分析
    php 变量定义方法
    1. 请问PHP里的ECHO是什么意思 ?请问PHP里的ECHO是什么意思???有什么作用???又应该怎么使用???
    谈谈如何优化MYSQL数据库查询
    MySQL存储引擎Innodb和MyISAM对比总结
    MYSQL的索引类型:PRIMARY, INDEX,UNIQUE,FULLTEXT,SPAIAL 有什么区别?各适用于什么场合?
    计算机网络基础知识总结
  • 原文地址:https://www.cnblogs.com/shuimuqingyang/p/10159168.html
Copyright © 2020-2023  润新知