Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7719071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:19:20+00:00 2026-06-01T03:19:20+00:00

With the below code I receive IOError: [Errno 13] Permission denied , and I

  • 0

With the below code I receive IOError: [Errno 13] Permission denied, and I know this is due to the output directory being a sub-folder 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 the modified files to a sub-folder of the input directory. How would I do this without getting the ‘Permission denied’ error? Would the tempfile module be useful in this scenario?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:19:22+00:00Added an answer on June 1, 2026 at 3:19 am

    os.listdir will return directory as well as file names. output is within inputdir so the with is trying to open a directory for reading/writing.

    What exactly are you trying to do? path, dirs, files aren’t even being used in the recursive os.walk.

    Edit: I think you’re looking for something like this:

    import os
    
    INPUTDIR= "c:\\temp2\\CSV"
    OUTPUTDIR = "c:\\temp2\\CSV\\output"
    keyword = "KEYWORD"
    
    def make_path(p):
        '''Makes sure directory components of p exist.'''
        try:
            os.makedirs(p)
        except OSError:
            pass
    
    def dest_path(p):
        '''Determines relative path of p to INPUTDIR,
           and generates a matching path on OUTPUTDIR.
        '''
        path = os.path.relpath(p,INPUTDIR)
        return os.path.join(OUTPUTDIR,path)
    
    make_path(OUTPUTDIR)
    
    for path, dirs, files in os.walk(INPUTDIR):
        for d in dirs:
            dir_path = os.path.join(path,d)
            # Handle case of OUTPUTDIR inside INPUTDIR
            if dir_path == OUTPUTDIR:
                dirs.remove(d)
                continue
            make_path(dest_path(dir_path))    
        for f in files:
            file_path = os.path.join(path, f)
            out_path = dest_path(file_path)
            with open(file_path, "r") as fh, open(out_path, "w") as fo:
                for line in fh:
                    if keyword not in line:
                        fo.write(line)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the below code to receive the message via serial port which
I'm using the below code to receive the messages using serial port event handler.
While running the code I've included below I receive the error EntityCommandExecutionException was unhandled
I am using the below code to receive the messages from serial port using
When I execute the code below I receive the error message: LINQ to Entities
Consider the code below: this.usedIds = 0; this.SendData = function(data) { var id =
The code below is what I am using to send and receive information from
EDIT: the code below has been fixed to receive and send properly AND to
I'm having an issue with a TCPListener. I have created this code below and
This seems to be trivial but here I am anyway. The code below is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.