python: write or read text file

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# coding=utf-8
"""
ReadWriteFile.py
读写文件
date 2023-06-17
edit: Geovin Du,geovindu, 涂聚文
ide:  PyCharm 2023.1 python 11
 
"""
 
class DuReadWirte(object):
    """
    简单读写TEXT文件
    """
 
    def WriteTextFile(content:str,filename:str):
        """
 
        :param content:
        :param filename:
        :return:
        """
        try:
            with open(filename, "w", encoding="utf-8") as f:
                f.write(content)
        except OSError:
            # 'File not found' error message.
            print("文件没有写成功!")
 
    def WriteTextPtyhonFile(content:str,filename:str):
        """
 
        :param content:
        :param filename:
        :return:
        """
        try:
            with open(filename, "w", encoding="utf-8") as f:
                f.write(content)
        except OSError:
            # 'File not found' error message.
            print("文件没有写成功!")
 
    def ReadTextFile(filename:str):
        """
 
        :param filename:
        :return:
        """
        try:
            with open(filename, "r", encoding="utf-8") as f:
                text = f.read()
                print(text)
                return text
        except OSError:
            # 'File not found' error message.
            return ""
            print("文件没有找到!")

  

调用:

1
2
3
4
5
ReadWriteFile.DuReadWirte.WriteTextFile(f"hi,Geovin Du,涂聚文,你好吗?안녕하세요?塗聚文、お元気ですか?",r"geovindu.txt") #写文件
pythonfile="import xlrd\t\nimport xlwt\t\nimport xlwings as xw\t\nimport xlsxwriter\t\nimport openpyxl as ws\t\nimport pandas as pd\t\nimport pandasql\t\nfrom pandasql import sqldf\t\nimport os\t\nimport sys\t\nfrom pathlib import Path\t\nimport re".join("import pyspark\t\nimport Insurance\t\nimport ReadExcelData\t\nimport SQLServerDAL\t\nimport MySQLDAL\t\nimport BookKind\t\nimport MySQLHelper\t\nimport SQLServerDAL\t\nimport MsSQLHelper\t\nimport binascii\t\nimport ReadWriteFile")
ReadWriteFile.DuReadWirte.WriteTextPtyhonFile(pythonfile,"geovindutest.py")
 
print(ReadWriteFile.DuReadWirte.ReadTextFile(r"geovindu.txt"))  #读文件

  

posted @   ®Geovin Du Dream Park™  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2015-06-17 csharp:Conversion Between DataTable and List
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示