代码未输出到正确的文件夹Python
因此,我有一段代码,可以打开一个文本文件,其中包含指向以下文件的路径列表: C:/Users/User/Desktop/mini_mouse/1980 C:/Users/User/Desktop/mini_mouse/1982 C:/Users/User/Desktop/mini_mouse/1984 然后逐行单独打开这些文件,并对文件进行过滤。然后我希望它将结果输出到一个完全不同的文件夹,名为:代码未输出到正确的文件夹Python,python,file,output,file-handling,Python,File,Output,File Handling,因此,我有一段代码,可以打开一个文本文件,其中包含指向以下文件的路径列表: C:/Users/User/Desktop/mini_mouse/1980 C:/Users/User/Desktop/mini_mouse/1982 C:/Users/User/Desktop/mini_mouse/1984 然后逐行单独打开这些文件,并对文件进行过滤。然后我希望它将结果输出到一个完全不同的文件夹,名为: output_location = 'C:/Users/User/Desktop/test2/'
output_location = 'C:/Users/User/Desktop/test2/'
目前,我的代码将结果输出到原始文件的打开位置,即如果它打开文件C:/Users/User/Desktop/mini_mouse/1980,则输出将位于同一文件夹中,名称为“1980_filtered”。一、 但是,希望输出进入输出位置。有人能看出我现在哪里出了问题吗?任何帮助都将不胜感激!这是我的密码:
import os
def main():
stop_words_path = 'C:/Users/User/Desktop/NLTK-stop-word-list.txt'
stopwords = get_stop_words_list(stop_words_path)
output_location = 'C:/Users/User/Desktop/test2/'
list_file = 'C:/Users/User/Desktop/list_of_files.txt'
with open(list_file, 'r') as f:
for file_name in f:
#print(file_name)
if file_name.endswith('\n'):
file_name = file_name[:-1]
#print(file_name)
file_path = os.path.join(file_name) # joins the new path of the file to the current file in order to access the file
filestring = '' # file string which will take all the lines in the file and add them to itself
with open(file_path, 'r') as f2: # open the file
print('just opened ' + file_name)
print('\n')
for line in f2: # read file line by line
x = remove_stop_words(line, stopwords) # remove stop words from line
filestring += x # add newly filtered line to the file string
filestring += '\n' # Create new line
new_file_path = os.path.join(output_location, file_name) + '_filtered' # creates a new file of the file that is currenlty being filtered of stopwords
with open(new_file_path, 'a') as output_file: # opens output file
output_file.write(filestring)
if __name__ == "__main__":
main()
假设您使用的是Windows(因为您有一个普通的Windows文件系统),则必须在路径名中使用反斜杠。请注意,这仅适用于Windows。我知道这很烦人,所以我给你换了(不客气:))。您还必须使用两个反斜杠,因为它将尝试将其用作转义字符
import os
def main():
stop_words_path = 'C:\\Users\\User\\Desktop\\NLTK-stop-word-list.txt'
stopwords = get_stop_words_list(stop_words_path)
output_location = 'C:\\Users\\User\\Desktop\\test2\\'
list_file = 'C:\\Users\\User\\Desktop\\list_of_files.txt'
with open(list_file, 'r') as f:
for file_name in f:
#print(file_name)
if file_name.endswith('\n'):
file_name = file_name[:-1]
#print(file_name)
file_path = os.path.join(file_name) # joins the new path of the file to the current file in order to access the file
filestring = '' # file string which will take all the lines in the file and add them to itself
with open(file_path, 'r') as f2: # open the file
print('just opened ' + file_name)
print('\n')
for line in f2: # read file line by line
x = remove_stop_words(line, stopwords) # remove stop words from line
filestring += x # add newly filtered line to the file string
filestring += '\n' # Create new line
new_file_path = os.path.join(output_location, file_name) + '_filtered' # creates a new file of the file that is currenlty being filtered of stopwords
with open(new_file_path, 'a') as output_file: # opens output file
output_file.write(filestring)
if __name__ == "__main__":
main()
假设您使用的是Windows(因为您有一个普通的Windows文件系统),则必须在路径名中使用反斜杠。请注意,这仅适用于Windows。我知道这很烦人,所以我给你换了(不客气:))。您还必须使用两个反斜杠,因为它将尝试将其用作转义字符
import os
def main():
stop_words_path = 'C:\\Users\\User\\Desktop\\NLTK-stop-word-list.txt'
stopwords = get_stop_words_list(stop_words_path)
output_location = 'C:\\Users\\User\\Desktop\\test2\\'
list_file = 'C:\\Users\\User\\Desktop\\list_of_files.txt'
with open(list_file, 'r') as f:
for file_name in f:
#print(file_name)
if file_name.endswith('\n'):
file_name = file_name[:-1]
#print(file_name)
file_path = os.path.join(file_name) # joins the new path of the file to the current file in order to access the file
filestring = '' # file string which will take all the lines in the file and add them to itself
with open(file_path, 'r') as f2: # open the file
print('just opened ' + file_name)
print('\n')
for line in f2: # read file line by line
x = remove_stop_words(line, stopwords) # remove stop words from line
filestring += x # add newly filtered line to the file string
filestring += '\n' # Create new line
new_file_path = os.path.join(output_location, file_name) + '_filtered' # creates a new file of the file that is currenlty being filtered of stopwords
with open(new_file_path, 'a') as output_file: # opens output file
output_file.write(filestring)
if __name__ == "__main__":
main()
根据您的代码,这似乎是一个问题:
new_file_path = os.path.join(output_location, file_name) + '_filtered'
在Python的os.path.join()中,输入中的任何绝对路径(或Windows中的驱动器号)都将丢弃它之前的所有内容,并从新的绝对路径(或驱动器号)重新启动连接。由于您直接从_files.txt的列表_调用文件_name,并且每个路径都是相对于C:驱动器格式化的,因此每次调用os.path.join()都会删除输出_位置并重置为原始文件路径
有关此行为的更好解释,请参阅
构建输出路径时,您可以从路径“C:/Users/User/Desktop/mini_mouse/1980”中删除文件名“1980”,并基于输出位置变量和独立文件名进行连接。基于您的代码,这似乎是一个问题:
new_file_path = os.path.join(output_location, file_name) + '_filtered'
在Python的os.path.join()中,输入中的任何绝对路径(或Windows中的驱动器号)都将丢弃它之前的所有内容,并从新的绝对路径(或驱动器号)重新启动连接。由于您直接从_files.txt的列表_调用文件_name,并且每个路径都是相对于C:驱动器格式化的,因此每次调用os.path.join()都会删除输出_位置并重置为原始文件路径
有关此行为的更好解释,请参阅
在构建输出路径时,您可以从路径“C:/Users/User/Desktop/mini_mouse/1980”中删除文件名“1980”,并根据输出位置变量和独立文件名进行连接。好的,我猜,您没有对任何文件调用
file.close()
,这是一个问题,但我不完全确定。试一试,告诉我是否适合你。好的。过滤后的\u做什么?它是否添加到文件名的末尾?好的,您需要将“.txt”与程序中的所有其他文件名一起添加到文件名的末尾,因为Python需要扩展名。尝试将“.txt”添加到所有引用的文件路径中。不,我的建议仅适用于文本文件。为什么要使用os.path.join()
?你不能把这些字符串连起来吗?像这样:new\u file\u path=output\u location+file\u name+“\u filtered.txt”
请记住,如果您使用此选项,file\u name
将必须没有扩展名。好吧,我猜一下,您没有对任何文件调用file.close()
,这是个问题,但我不能完全确定。试一试,告诉我是否适合你。好的。过滤后的\u做什么?它是否添加到文件名的末尾?好的,您需要将“.txt”与程序中的所有其他文件名一起添加到文件名的末尾,因为Python需要扩展名。尝试将“.txt”添加到所有引用的文件路径中。不,我的建议仅适用于文本文件。为什么要使用os.path.join()
?你不能把这些字符串连起来吗?如下所示:new\u file\u path=output\u location+file\u name+'\u filtered.txt'
请记住,如果使用此选项,file\u name
必须不带扩展名。