Python 如何将输出数据放入path1中指定的目录?
我想将输出文件写入添加到path1的目录中。但当我尝试这样做时,错误就会发生Python 如何将输出数据放入path1中指定的目录?,python,file-io,Python,File Io,我想将输出文件写入添加到path1的目录中。但当我尝试这样做时,错误就会发生 import os import sys path = 'ODOGS/LAB' path1= 'ODOGS/lab_res_voiced' for file in os.listdir(path): current = os.path.join(path, file) name_output=file #name of the current file must be the same for th
import os
import sys
path = 'ODOGS/LAB'
path1= 'ODOGS/lab_res_voiced'
for file in os.listdir(path):
current = os.path.join(path, file)
name_output=file #name of the current file must be the same for the output file
index_list_B = []
line_list_B = []
line_list_linije = []
with open(current) as f1:
for index1, line1 in enumerate(f1):
strings_B = ("p","P","t","T") #find lines which contain this str
line_list_B.append(line1) #put the line in line_list_B
if any(s in line1 for s in strings_B):
line_list_linije.append(line1)
print(line_list_linije)
index_list_B.append(index1) #positions of lines
with open(path1.join(name_output).format(index1), 'w') as output1:
for index1 in index_list_B:
print(line_list_B[index1])
output1.writelines([line_list_B[index11],
line_list_B[index1],','])
此代码通过'ODOGS/LAB'
目录中的文本文件,搜索是否有包含ceratan字符串的行。找到所有符合条件的行后,它需要将它们写入新文件中,但名称与输入文件相同。代码的这一部分工作得很好
我想将所有输出文件放入另一个目录path1
,但
with
语句不起作用
我得到一个错误:
FileNotFoundError: [Errno 2] No such file or directory: 'sODOGS/lab_res_voicedzODOGS...
当with
语句为:
with open(name_output.format(index1), 'w') as output1:
但是我得到了根文件夹中我不想要的所有文件。
我的问题是如何将输出文件放入
路径1
中的目录?形成输出路径时出错:
而不是
with open(path1.join(name_output).format(index1), 'w') as output1:
你想要
with open(os.path.join(path1, name_output), 'w') as output1:
形成输出路径时出错: 而不是
with open(path1.join(name_output).format(index1), 'w') as output1:
你想要
with open(os.path.join(path1, name_output), 'w') as output1: