我有以下代码应该遍历文件夹中的所有文件,但是它无法遍历所有文件。我添加了那里的参数来忽略读取错误,但仍然失败。 我尝试了各种读写模式-“ rb”,“ wb”,“ w +”无济于事。 有什么想法吗?
import os
import csv
import glob
cwd = os.getcwd()
directory = cwd
output = cwd
txt_files = os.path.join(directory, '*.txt')
for txt_file in glob.glob(txt_files):
with open(txt_file, "r", errors = 'ignore') as input_file:
in_txt = csv.reader(input_file, delimiter='|')
filename = os.path.splitext(os.path.basename(txt_file))[0] + '.csv'
with open(os.path.join(output, filename), 'w+') as output_file:
out_csv = csv.writer(output_file)
out_csv.writerows(in_txt)