发布时间:2023-10-30 09:00
import os
target_dir = r'D:\works\文本分析\leopythonbookdata-master\texts'
files = os.listdir(target_dir)
for file in files:
if file.endswith(r'.txt'):
print(file)
import os
target_dir = 'D:/works/文本分析/leopythonbookdata-master/texts/'
files = os.listdir(target_dir)
file_out = open(target_dir + 'files_combined.txt','a',encoding='utf8')
for file in files:
if file.endswith(r'.txt'):
file_in = open(target_dir + file,'r',encoding='ISO-8859-1')#这里没有encoding,亦或用'utf8'编码都会出现报错
text = file_in.readlines()
for t in text:
file_out.write(t)
file_in.close()
file_out.close()
import os
rootdir = 'D:/works/文本分析/leopythonbookdata-master/texts/'
allfiles = []
for root,subFolder,files in os.walk(rootdir):
for file in files:
allfiles.append(os.path.join(root,file))
for i in allfiles:
if i.endswith('.txt'):
print(i)
for root,subFolder,files in os.walk(rootdir):
# print('root:',root)
# print('subFolders:',subFolder)
# print('files:',files,'\n')
import os
rootdir = 'D:/works/文本分析/leopythonbookdata-master/texts/'
allfiles = []
file_out = open(rootdir + 'allfiles_combined.txt','a',encoding='utf8')
for root,subFolder,files in os.walk(rootdir):
for file in files:
allfiles.append(os.path.join(root,file))
for i in allfiles:
if i.endswith('.txt'):
file_in = open(i,'r',encoding='ISO-8859-1')
for line in file_in.readlines():
file_out.write(line)
file_in.close()
file_out.close()
import os
target_dir = 'D:/works/文本分析/leopythonbookdata-master/texts/temp_folder/'
files = os.listdir(target_dir)
for file in files:
if file.endswith(".txt"):
# open the txt file
file_in = open(target_dir + file,'r',encoding='ISO-8859-1')
text = file_in.readlines()
# open the new txt file with file name changed
file_out = open(target_dir + file[:-4] + "_changed.txt",'a')
for t in text:
file_out.write(t)
file_in.close()
file_out.close()