简体   繁体   English

权限错误Errno13-Windows上的Python

[英]Permission Error Errno13 - Python on Windows

thank you for your help in advance. 提前谢谢你的帮助。

I am writing some code for going through multiple pdfs in different folders and looking for specific words. 我正在编写一些代码,以便在不同的文件夹中浏览多个pdf文件并查找特定的单词。 My python knowledge is rudimentary at best as I am only learning it for my bachelor thesis. 我的python知识充其量是最基本的知识,因为我只是为我的学士论文而学习。

The code works fine when I run it within the folder itself, but I am not trying to get it to automatically run through every subfolder in a certain folder. 当我在文件夹本身中运行该代码时,它可以正常工作,但是我并不想使它自动在某个文件夹中的每个子文件夹中运行。

import PyPDF2
import os
rootdir = r"C:\Users\Tim Knickmann\Documents\LUBS\(3300) Dissertation\Data\Python Scripts for Earnigns Calls\Germany Transcripts"
extensions = ('.pdf')
pronoun_file = r"C:\Users\Tim Knickmann\Documents\LUBS\(3300) Dissertation\Data\Python Scripts for Earnigns Calls\pronoun_use.txt"
first_person_pronoun_file = r"C:\Users\Tim Knickmann\Documents\LUBS\(3300) Dissertation\Data\Python Scripts for Earnigns Calls\first_per_pronoun_use.txt"


def average_use(lst):
    return sum(lst) / float(len(lst))

# running it for every file
for subdirs_1, dirs_1, files_1 in os.walk(rootdir):
  for subdirs_1 in dirs_1:
        working_folder_directory = os.path.join(rootdir, subdirs_1)

        # reading in file into a seperate text document
        for subdirs_2, dirs_2, files_2 in os.walk(working_folder_directory):
            list_first_person_usage = []
            pdfFileObj = open(subdirs_2, 'rb')
            pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
            with open('working_doc.txt', 'w', encoding="utf-8") as f:
                        for i in range(0,pdfReader.numPages) :
                           pageObj = pdfReader.getPage(i)
                           f.write(pageObj.extractText())

Whenever I run the code it returns this error log: 每当我运行代码时,它都会返回以下错误日志:

    runfile('C:/Users/Tim Knickmann/Documents/LUBS/(3300) Dissertation/Data/Python Scripts for Earnigns Calls/Germany Transcripts/190319 v10 Script for Earnings Calls.py', wdir='C:/Users/Tim Knickmann/Documents/LUBS/(3300) Dissertation/Data/Python Scripts for Earnigns Calls/Germany Transcripts')
Traceback (most recent call last):

  File "<ipython-input-66-a9a93e480b59>", line 1, in <module>
    runfile('C:/Users/Tim Knickmann/Documents/LUBS/(3300) Dissertation/Data/Python Scripts for Earnigns Calls/Germany Transcripts/190319 v10 Script for Earnings Calls.py', wdir='C:/Users/Tim Knickmann/Documents/LUBS/(3300) Dissertation/Data/Python Scripts for Earnigns Calls/Germany Transcripts')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Tim Knickmann/Documents/LUBS/(3300) Dissertation/Data/Python Scripts for Earnigns Calls/Germany Transcripts/190319 v10 Script for Earnings Calls.py", line 24, in <module>
    pdfFileObj = open(subdirs_2, 'rb')

PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Tim Knickmann\\Documents\\LUBS\\(3300) Dissertation\\Data\\Python Scripts for Earnigns Calls\\Germany Transcripts\\Deutsche Wohnen'

I've parsed through what is available, but am unable to find anything which applies to this situation. 我已经解析了可用的内容,但是找不到适用于这种情况的任何内容。

I'm fairly certain that I am trying to open an already open file, but cannot figure out another way. 我可以肯定地说,我正在尝试打开一个已经打开的文件,但是找不到其他方法。

All help is greatly appreciated so thanks again. 非常感谢所有帮助,再次感谢。

As the error shows, on the line: 如错误所示,在行上:

pdfFileObj = open(orginial_file_directory, 'rb')

orginial_file_directory has the value orginial_file_directory具有值

C:\\Users\\Tim Knickmann\\Documents\\LUBS\\(3300) Dissertation\\Data\\Python Scripts for Earnigns Calls\\Germany Transcripts

which makes sense, because you have set it to be 这很有意义,因为您已将其设置为

orginial_file_directory = os.path.dirname(os.path.realpath(file))

As the variable name suggests, you understand, that this is a directory which you can of course not open as a file. 正如变量名所暗示的那样,您了解这是一个目录 ,您当然不能将其作为文件打开。

I think you want to do something like 我想你想做些类似的事情

pdfFileObj = open(file, 'rb')

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

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