如何从文件夹中读取并保存在 Python 中的另一个文件夹中
How do I make to read from folder and save in another folder in Python
此代码有效,但我必须一个一个地调用所有文件,我只需要调用文件所在的文件夹并将结果保存在另一个文件夹中。
我不知道 :( 谁能帮助我,我是 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
函数指定要打印到的位置),因此当您遍历文件时,您可以读取它们并通过工作代码删除它们。
此代码有效,但我必须一个一个地调用所有文件,我只需要调用文件所在的文件夹并将结果保存在另一个文件夹中。 我不知道 :( 谁能帮助我,我是 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
函数指定要打印到的位置),因此当您遍历文件时,您可以读取它们并通过工作代码删除它们。