python3文本读取与写入常用代码

创建文件夹:
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:
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'等。

写入文本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!'


读取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)
 
读取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的转置。
 
遍历文件夹:
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 返回的是当前文件下所有文件的绝对路径
原文地址:https://www.cnblogs.com/pscc/p/9562146.html