With the code below, I get an IOError: [Errno 13] Permission denied , and I know that this is because the output directory is a subfolder of the input directory:
import datetime import os inputdir = "C:\\temp2\\CSV\\" outputdir = "C:\\temp2\\CSV\\output\\" keyword = "KEYWORD" for path, dirs, files in os.walk(os.path.abspath(inputdir)): for f in os.listdir(inputdir): file_path = os.path.join(inputdir, f) out_file = os.path.join(outputdir, f) with open(file_path, "r") as fh, open(out_file, "w") as fo: for line in fh: if keyword not in line: fo.write(line)
However, when I change the output folder to: outputdir = "C:\\temp2\\output\\" , the code runs successfully. I want to be able to write modified files to a subfolder of the input directory. How can I do this without receiving the "Allow Denial" error? Can the tempfile module be useful in this scenario?
source share