(技术)Python 读文件 与 写文件

读文件


def writeTextFile():
   try:
       content = 'Hello,xk,啊啊啊'
       content.encode('UTF-8')
       f = open('E:\\test.txt','w') # w 代表读
       f.write(content)
   except BaseException as e:
       print(e)
   finally:
       if f:
           f.close()

# 调用:
if __name__ == '__main__':
   writeTextFile()

读文件:


def readFile():
    try:
        f = open('E:\\test.txt','r') # r 代表读
        # print(f.read())    #  read()会⼀次性读取⽂件的全部内容
        print(f.readlines())  # 每次读取⼀⾏内容,返回list
    except BaseException as e:
        print(e)
    finally:
        if f :
            f.close()

# 调用:
if __name__ == '__main__':
    readFile()

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容