提交 1b2008dc 编写于 作者: 1_bit's avatar 1_bit

Add new file

上级 85758e81
import os
class FileHandle:
#获取文件内容
def get_content(self,path):
f = open(path,"r") #设置文件对象
content = f.read() #将txt文件的所有内容读入到字符串str中
f.close() #将文件关闭
return content
#获取文件内容
def get_text(self):
file_path=os.path.dirname(__file__) #当前文件所在目录
txt_path=file_path+r'\textsrc' #txt目录
rootdir=os.path.join(txt_path) #目标目录内容
local_text={}
# 读txt 文件
for (dirpath,dirnames,filenames) in os.walk(rootdir):
for filename in filenames:
if os.path.splitext(filename)[1]=='.txt':
flag_file_path=dirpath+'\\'+filename #文件路径
flag_file_content=self.get_content(flag_file_path) #读文件路径
if flag_file_content!='':
local_text[filename.replace('.txt', '')]=flag_file_content #键值对内容
return local_text
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册