如何遍历文件夹中的每个文件,对文件执行一些操作并将输出保存到另一个文件夹中的文件 Python
How to loop through each file in a folder, do some action to the file and save output to a file in another folder Python
我有一个包含多个文件的文件夹,如下所示:
1980
1981
1982
这些文件中的每一个都有一些文本。我想遍历每个文件并对每个文件执行一些操作,然后将编辑后的文件保存到另一个文件夹并移至下一个文件,依此类推。结果将是我有原始文件夹,然后是另一个文件夹,其中包含每个文件的编辑版本,如下所示:
1980_filtered
1981_filtered
1982_filtered
可以这样做吗?
目前我有一些代码循环遍历文件夹中的文件,对每个文件进行一些过滤,然后将每个文件的所有编辑保存到一个大文件中。这是我的代码:
import os
input_location = 'C:/Users/User/Desktop/mini_mouse'
output_location = 'C:/Users/User/Desktop/filter_mini_mouse/mouse'
for root, dir, files in os.walk(input_location):
for file in files:
os.chdir(input_location)
with open(file, 'r') as f, open('NLTK-stop-word-list', 'r') as f2:
mouse_file = f.read().split() # reads file and splits it into a list
stopwords = f2.read().split()
x = (' '.join(i for i in mouse_file if i.lower() not in (x.lower() for x in stopwords)))
with open(output_location, 'a') as output_file:
output_file.write(x)
如有任何帮助,我们将不胜感激!
您需要指定每个新文件的名称。为此,Python 有一些很好的字符串格式化方法。幸运的是,你想要的新文件名很容易在循环中完成
import os
input_location = 'C:/Users/User/Desktop/mini_mouse'
output_location = 'C:/Users/User/Desktop/filter_mini_mouse/mouse'
for root, dir, files in os.walk(input_location):
for file in files:
new_file = "{}_filtered.txt".format(file)
os.chdir(input_location)
with open(file, 'r') as f, open('NLTK-stop-word-list', 'r') as f2:
mouse_file = f.read().split()
stopwords = f2.read().split()
x = (' '.join(i for i in mouse_file if i.lower() not in (x.lower() for x in stopwords)))
with open(output_location+'/'+new_file, 'w') as output_file: # Changed 'append' to 'write'
output_file.write(x)
如果您使用的是 Python 3.7,则可以
new_file = f"{file}_filtered.txt"
和
with open(f"{output_location}/{new_file}", 'w') as output_file:
output_file.write(x)
首先你应该先打开 NLTK-stop-word-list
一次,所以我把它移到了你的循环之外。其次,os.chdir()
是多余的,您可以使用 os.path.join()
获取当前文件路径(并构建新文件路径):
import os
input_location = 'C:/Users/User/Desktop/mini_mouse'
output_location = 'C:/Users/User/Desktop/filter_mini_mouse/'
stop_words_path = 'C:/Users/User/Desktop/NLTK-stop-word-list.txt'
with open(stop_words_path, 'r') as stop_words:
for root, dirs, files in os.walk(input_location):
for name in files:
file_path = os.path.join(root, name)
with open(file_path, 'r') as f:
mouse_file = f.read().split() # reads file and splits it into a list
stopwords = stop_words.read().split()
x = (' '.join(i for i in mouse_file if i.lower() not in (x.lower() for x in stopwords)))
new_file_path = os.path.join(output_location, name) + '_filtered'
with open(new_file_path, 'a') as output_file:
output_file.write(x)
P.S:我冒昧地更改了您的一些变量名称,因为它们是 python 内置词('file' 和 'dir')的一部分.如果你 运行 __builtins__.__dict__.keys()
你会在那里看到它们。
我有一个包含多个文件的文件夹,如下所示:
1980
1981
1982
这些文件中的每一个都有一些文本。我想遍历每个文件并对每个文件执行一些操作,然后将编辑后的文件保存到另一个文件夹并移至下一个文件,依此类推。结果将是我有原始文件夹,然后是另一个文件夹,其中包含每个文件的编辑版本,如下所示:
1980_filtered
1981_filtered
1982_filtered
可以这样做吗?
目前我有一些代码循环遍历文件夹中的文件,对每个文件进行一些过滤,然后将每个文件的所有编辑保存到一个大文件中。这是我的代码:
import os
input_location = 'C:/Users/User/Desktop/mini_mouse'
output_location = 'C:/Users/User/Desktop/filter_mini_mouse/mouse'
for root, dir, files in os.walk(input_location):
for file in files:
os.chdir(input_location)
with open(file, 'r') as f, open('NLTK-stop-word-list', 'r') as f2:
mouse_file = f.read().split() # reads file and splits it into a list
stopwords = f2.read().split()
x = (' '.join(i for i in mouse_file if i.lower() not in (x.lower() for x in stopwords)))
with open(output_location, 'a') as output_file:
output_file.write(x)
如有任何帮助,我们将不胜感激!
您需要指定每个新文件的名称。为此,Python 有一些很好的字符串格式化方法。幸运的是,你想要的新文件名很容易在循环中完成
import os
input_location = 'C:/Users/User/Desktop/mini_mouse'
output_location = 'C:/Users/User/Desktop/filter_mini_mouse/mouse'
for root, dir, files in os.walk(input_location):
for file in files:
new_file = "{}_filtered.txt".format(file)
os.chdir(input_location)
with open(file, 'r') as f, open('NLTK-stop-word-list', 'r') as f2:
mouse_file = f.read().split()
stopwords = f2.read().split()
x = (' '.join(i for i in mouse_file if i.lower() not in (x.lower() for x in stopwords)))
with open(output_location+'/'+new_file, 'w') as output_file: # Changed 'append' to 'write'
output_file.write(x)
如果您使用的是 Python 3.7,则可以
new_file = f"{file}_filtered.txt"
和
with open(f"{output_location}/{new_file}", 'w') as output_file:
output_file.write(x)
首先你应该先打开 NLTK-stop-word-list
一次,所以我把它移到了你的循环之外。其次,os.chdir()
是多余的,您可以使用 os.path.join()
获取当前文件路径(并构建新文件路径):
import os
input_location = 'C:/Users/User/Desktop/mini_mouse'
output_location = 'C:/Users/User/Desktop/filter_mini_mouse/'
stop_words_path = 'C:/Users/User/Desktop/NLTK-stop-word-list.txt'
with open(stop_words_path, 'r') as stop_words:
for root, dirs, files in os.walk(input_location):
for name in files:
file_path = os.path.join(root, name)
with open(file_path, 'r') as f:
mouse_file = f.read().split() # reads file and splits it into a list
stopwords = stop_words.read().split()
x = (' '.join(i for i in mouse_file if i.lower() not in (x.lower() for x in stopwords)))
new_file_path = os.path.join(output_location, name) + '_filtered'
with open(new_file_path, 'a') as output_file:
output_file.write(x)
P.S:我冒昧地更改了您的一些变量名称,因为它们是 python 内置词('file' 和 'dir')的一部分.如果你 运行 __builtins__.__dict__.keys()
你会在那里看到它们。