简体   繁体   English

Python IOError:[Errno 13]权限被拒绝

[英]Python IOError: [Errno 13] Permission denied

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: 使用下面的代码,我收到IOError: [Errno 13] Permission denied ,我知道这是由于输出目录是输入目录的子文件夹:

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. 但是,当我将输出文件夹更改为: outputdir = "C:\\\\temp2\\\\output\\\\" ,代码将成功运行。 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? tempfile模块在这种情况下会有用吗?

os.listdir will return directory as well as file names. os.listdir将返回目录以及文件名。 output is within inputdir so the with is trying to open a directory for reading/writing. outputinputdir因此with试图打开一个目录进行读取/写入。

What exactly are you trying to do? 您到底想做什么? path, dirs, files aren't even being used in the recursive os.walk . path, dirs, files甚至都没有在递归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)

If you are successful in writing to a output directory outside of the input traversing directory, then write it there first using the same code as above and then move it to a sub-directory within the input directory. 如果您成功写入了输入遍历目录之外的输出目录,则首先使用与上述相同的代码将其写入那里,然后将其移至输入目录内的子目录。 You could use os.move for that. 您可以使用os.move

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM