Tim
Tim

Reputation: 21

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. My python knowledge is rudimentary at best as I am only learning it for my bachelor thesis.

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.

Upvotes: 2

Views: 2330

Answers (1)

FlyingTeller
FlyingTeller

Reputation: 20472

As the error shows, on the line:

pdfFileObj = open(orginial_file_directory, 'rb')

orginial_file_directory has the value

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')

Upvotes: 1

Related Questions