Python:为什么我会收到 [Errno 13] 权限被拒绝?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14224013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:48:17  来源:igfitidea点击:

Python: Why am I getting [Errno 13] Permission Denied?

python

提问by

I am trying to write some code that searches a group of files for a string and then places any files that include the string into a different directory which at the start of the program will not exist.

我正在尝试编写一些代码来搜索一组文件中的字符串,然后将包含该字符串的任何文件放入程序开始时不存在的不同目录中。

import os
import re

def test():
    os.chdir("C:/Users/David/Files/TestFiles")
    files = os.listdir(".")
    os.mkdir("C:/Users/David/Files/TestFiles2")
    for x in (files):
        inputFile = open((x), "r")
        content = inputFile.read()
        inputFile.close()
        if "Hello Word" in content:
            with open ("C:/Users/David/Files/TestFiles2", "w") as outputFile:
                outputFile.write(content)

When it runs I get the following error message

当它运行时,我收到以下错误消息

PermissionError: [Errno 13] Permission denied: 'C:/Users/David/Files/TestFiles2'

Just wondering if anyone can tell me why this error message appears.

只是想知道是否有人可以告诉我为什么会出现此错误消息。

采纳答案by Martijn Pieters

You cannot open a directory for writing:

您无法打开目录进行写入:

os.mkdir("C:/Users/David/Files/TestFiles2")

followed by

其次是

open("C:/Users/David/Files/TestFiles2", "w")

won't work. Did you mean to add a filename to the latter statement?

不会工作。您的意思是在后一条语句中添加文件名吗?

If that was to be based on the xfilename, you should add that name to the path:

如果这是基于x文件名,您应该将该名称添加到路径中:

with open(os.path.join("C:/Users/David/Files/TestFiles2", os.path.basename(x)), 'w') as outputFile:

To movethe file, use shutil.move()instead; no need to 'open' the directory for that:

移动文件,请shutil.move()改用;无需为此“打开”目录:

if "facebook.com" in content.lower():
     shutil.move(x, "C:/Users/David/Files/TestFiles2")

Complete code, simplified:

完整代码,简化:

def test():
    src = "C:/Users/David/Files/TestFiles"
    dst = "C:/Users/David/Files/TestFiles2"
    os.mkdir(dst)
    for filename in os.listdir(src):
        path = os.path.join(src, filename)
        with open(path, "r") as inputFile:
            content = inputFile.read()
        if "facebook.com" in content.lower():
            shutil.move(path, dst)

回答by pvoosten

You will select all files with that if statement:

您将使用该 if 语句选择所有文件:

"XXX" or "YYY" in "ZZZ"always evaluates to True. It is the same as ("XXX") or ("YYY" in "ZZZ"). Since a nonempty string evaluates to True, you will select all files.

"XXX" or "YYY" in "ZZZ"始终评估为 True。它与 相同 ("XXX") or ("YYY" in "ZZZ")。由于非空字符串的计算结果为 True,因此您将选择所有文件。

EDIT:

编辑:

if "facebook.com" or "Facebook.com" in content:
    ...

It will seem like all files contain "facebook.com"or "Facebook.com", but that's only because "facebook.com"in itself evaluates to True. "Facebook.com" in contentwill never even be checked. The if statement is functionally equal to

看起来所有文件都包含"facebook.com"or "Facebook.com",但这只是因为"facebook.com"它本身的计算结果为 True。"Facebook.com" in content甚至永远不会被检查。if 语句在功能上等同于

if "facebook.com":
    ...

and the if block will be executed. What you actually meant was

并且 if 块将被执行。你真正的意思是

if ("facebook.com" in content) or ("Facebook.com" in content):
    ...

That's that. And if you don't want to search all content twice, you can use regular expressions, but that's a different topic.

就是这样。如果您不想搜索所有内容两次,您可以使用正则表达式,但这是一个不同的主题。