美文网首页
Python读取写入获取文件简单例子

Python读取写入获取文件简单例子

作者: 爱学习的蹭蹭 | 来源:发表于2022-11-17 12:03 被阅读0次

Python读取写入获取文件简单例子

# 对个获取指定目录的所有文件
def get_all_files(dir):
    fileList = []
    """遍历获取指定文件夹下面所有文件"""
    if os.path.isdir(dir):
        filelist = os.listdir(dir)
        for ret in filelist:
            filename = dir + "\\" + ret
            if os.path.isfile(filename):
                fileList.append(filename)
    return fileList


# 对个获取指定目录的所有文件
def get_file_list_by_walk(dir):
    fileList = []
    """使用listdir循环遍历"""
    if not os.path.isdir(dir):
        return fileList
    dirlist = os.walk(dir)
    for root, dirs, files in dirlist:
        for fi in files:
            fileList.append(os.path.join(root, fi))
    return fileList

#指定文件路径获取文件最后文件的路径包含文件
#如:D:\test\file.txt 返回的结果为:D:\test\
def get_file_path(path):
    #获取文件名
    #return os.path.split(path)[1] 
    # 获取文件路径
    return os.path.split(path)[0]

#指定文件路径获取文件最后文件的路径包含文件
#如:D:\test\file.txt 返回的结果为:file.txt
def get_file_name(path):
    return os.path.basename(path)
    
# 创建目录
def mkdir(path):
    # 去除尾部 \ 符号
    pathx = path.strip().rstrip("\\")
    # 判断路径是否存在
    # 存在     True
    # 不存在   False
    isExists = os.path.exists(pathx)
    # 判断结果
    if not isExists:
        # 如果不存在则创建目录创建目录操作函数
        os.makedirs(path)
        print(path + ' 创建成功')
        return True
    else:
        # 如果目录存在则不创建,并提示目录已存在
        print(path + ' 目录已存在')
        return False


# 创建一个txt文件,并向文件写入msg
"""
@file_dir参数 代表文件目录 如:D:\test
@file_name参数 代表文件名称 如:file.txt
@msg参数 代表要写入文件的内容信息
"""
def writer_to_file(file_dir, file_name, msg):
    # 先创建目录
    mkdir(file_dir)
    # 再打开文件
    full_path = file_dir + "\\" + file_name
    fi = open(full_path, 'w')
    # 写入文件
    fi.write(msg) 
    fi.close()

"""
通过指定文件路径文件进行读取内容
如:D:\test\file.txt
"""
def reader_file(path):
    #解决乱码问题
    fi = open(path,'r',encoding='gbk',errors = 'ignore')
    strs=[]
    #splitlines解决不换行\n输出
    for line in fi.read().splitlines():
        if(len(line)>0):
            strs.append(line)
    return strs



   
#测试__main__方法
if __name__ == '__main__':
    path = r"D:\test\test.txt"
    #读取内容
    fileStr = reader_file(path)
    
    # 最终统计的信息写入文件
    writer_to_file(path, "file.txt", fileStr)
    
    print('do finish')

相关文章

  • Python读取写入获取文件简单例子

    Python读取写入获取文件简单例子

  • fs文件系统操作

    基础写入文件 简单写入文件 流式文件写入 简单文件读取 流式文件读取 流式文件拷贝(读取 + 写入) 复制文件 f...

  • 使用Pandas读取csv文件

    python读取csv文件简单例子: python读取csv文件时,数据被保存到dataframe中,此时,数据会...

  • 2018-09-14 数据库连接

    前面介绍过python中文件的读取和写入,利用文件读写可以将python程序产生的数据保持到文件中 但简单的纯文本...

  • 2019-04-02--window 下练习python

    window 下练习python 按行读取文件: 同时把读入的文件写入另一文件 由于Python写入文件时,并不是...

  • nodejs 基础模块 fs

    fs 文档 fs 文件系统 处理文件的模块 fs.readFile 读取文件 例子 fs.writeFile 写入...

  • python 文件操作

    fp=open("文件路径","方式") 文件读取 文件写入 文件关闭 文件读取写入方式

  • 2018-03-13

    文件与异常 读取与写入 python可以读取打开文件并读取其内容如下面代码 使用with可以在你不需要继续访问文件...

  • Python 中更优雅的异常处理方案

    Python 中更优雅的异常处理方案 实例引入 比如写 Python 的时候,举个最简单的算术运算和文件写入的例子...

  • cordova实现文件系统的读写

    1.获取Document目录 2.创建目录 3.获取文件 4.获取文件夹内所有文件 5.写入文件 6.读取文件内容...

网友评论

      本文标题:Python读取写入获取文件简单例子

      本文链接:https://www.haomeiwen.com/subject/fmkpxdtx.html