赞
踩
- def ReadFile(filename,mode = 'r'):
- """
- 读取文件内容
- @filename 文件名
- return string(bin) 若文件不存在,则返回空字符串
- """
-
- import os
- if not os.path.exists(filename):return""
-
- fp = open(filename,mode,encoding="utf-8")
- f_body = fp.read()
- fp.close()
- return f_body
-
- #读取文本文件
- txt = ReadFile("员工信息.txt")
- print(txt)
'运行
运行结果:
- def WriteFile(filename,s_body,mode='w+'):
- """
- 写入文件内容
- @filename 文件名
- @s_body 欲写入的内容
- """
-
- fp = open(filename,mode)
- fp.write(s_body)
- fp.close()
-
- #写入文本文件
- WriteFile("1.txt","Hello World!")
'运行
运行结果:
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。