IUpdatable

[Python] iupdatable包:File模块使用介绍

一、简介

文件模块主要是对常见的文件读写功能进行了封装,默认使用UTF8(utf_8_sig)格式编码,实现一行代码读写文件。

 

二、简单示例

安装 iupdatable 包

pip install --upgrade iupdatable

使用实例:

from iupdatable.system.io.File import File


sample_text = 'this is sample text.'
sample_texts = ['123', 'abc', 'ABC']
append_text = 'this is append text.'

# 写入
File.write('1.txt', sample_text)
File.write_lines('2.txt', sample_texts)

File.append('1.txt', append_text)
File.append_new_line('2.txt', append_text)

# 读取
read_text1 = File.read('1.txt')
read_text2 = File.read_lines('2.txt')

# 打印输出
print(read_text1)
print(read_text2)

输出:

this is sample text.this is append text.
['123', 'abc', 'ABC', 'this is append text.']

 

三、补充说明

 

所有文件名应该是包含目录的完整文件路径;

所有的写入相关的操作,对于文件不存在的情况,均会先新建文件,然后写入;

 

File类下的全部函数:

  • read: 读取文件
  • write: 写入文件
  • append:追加写入文件
  • append_new_line:新建一行,然后追加写入文件
  • read_lines: 按行一次性读取文件
  • write_lines:按行一次性写入文件
  • write_csv:写入CSV文件
  • read_csv:读取CSV文件
  • exist_within_extensions: 检查一个文件是否存在(在指定的几种格式中)
  • get_file_path_within_extensions: 获取一个文件的路径(在指定的几种格式中)

 

分类:

技术点:

相关文章:

  • 2021-12-27
  • 2021-12-09
  • 2020-01-25
  • 2019-11-15
  • 2021-11-02
  • 2021-11-02
  • 2022-01-12
  • 2021-10-10
猜你喜欢
  • 2020-03-15
  • 2020-12-26
  • 2021-03-25
  • 2021-10-21
  • 2021-07-30
  • 2021-12-16
  • 2021-09-05
相关资源
相似解决方案