问题描述
此代码有效,但是我必须一个一个地调用所有文件,我只需要调用文件所在的文件夹并将结果保存在另一个文件夹中。 我没有弄清楚:(有人可以帮我吗,我是Python新手。谢谢,我很感激:)
import re
import string
import sys
frequency = {}
sys.stdin = open('C:/Users/Desktop/app/data/sources/books/test.txt','r')
sys.stdout =open('C:/Users/Desktop/app/data/fre/news/test.txt','w')
text_string = sys.stdin.read()
match_pattern = re.findall(r'([-][\w]+)',text_string)
for word in match_pattern:
count = frequency.get(word,0)
frequency[word] = count + 1
frequency_list = frequency.keys()
for word in frequency_list:
print (word,frequency[word])
解决方法
也许是这样吗?
import glob
import os
books = glob.glob("C:/Users/Desktop/app/data/sources/books/*.txt")
# now you have a list of all .txt files in that directory.
def writer(text_string,output_file):
"""A function to write out items from an input text string"""
frequency = {}
match_pattern = re.findall(r'([-][\w]+)',text_string)
for word in match_pattern:
count = frequency.get(word,0)
frequency[word] = count + 1
frequency_list = frequency.keys()
for word in frequency_list:
print(word,frequency[word],file=open(output_file,"a"))
# now you have a function that essentially does the procedure you already know works
for book in books:
book_name = os.path.split(book)[-1] # get <filename>.txt from the path
# context manager will close the stream when you're done
with open(book,"r") as file:
text_string = file.read()
output_file = "C:/Users/Desktop/app/data/fre/news/" + book_name
writer(text_string,output_file)
此代码将遍历您正在读取的目录中的.txt
文件。
我将您的工作代码封装在一个函数中(为清晰起见,已重新格式化,您可以指定要从print
函数直接打印到的位置),以便在遍历文件时可以读取并拖放它们通过工作代码。