"PermissionError: [Errno 13] Permission denied"in windows

I have already run anaconda as an administrator, but it still shows “PermissionError: [Errno 13] Permission denied”.
I thought it was because the folder attribute is read-only, so i tried several ways to change this such as CMD checks and powershell checks but the folder still shows read-only. The windows support said that this was not the issue and the display of read-only is something can’t be changed.
So what am i suppose to do next. Thanks.

What code are you executing to get this error?

import sys
print(sys.executable)
import wave
import json
import os
import threading
import sounddevice as sd
from scipy.io.wavfile import write
import keyboard
from vosk import Model, KaldiRecognizer
import numpy as np

standard_text = "hello python hello world"

def record_audio(file_path):
    fs = 16000

    save_dir = os.path.dirname(file_path)
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
        print(f"Directory created: {save_dir}")
    else:
        print(f"Directory already exists: {save_dir}")

    recording = threading.Event()

    def on_press(key):
        nonlocal recording
        if key.name == 'esc':
            print("Start recording...")
            recording.set()
        elif key.name == 'enter':
            print("Stop recording...")
            recording.clear()

    keyboard.on_press(on_press)

    print("Press ESC key to start recording, press Enter key to stop...")
    recording.wait()

    frames = []

    def callback(indata, frames, time, status):
        frames.append(indata.copy())

    with sd.InputStream(samplerate=fs, channels=1, callback=callback):
        recording.wait()

    if len(frames) > 0:
        write(file_path, fs, np.concatenate(frames))
        print(f"File saved at: {file_path}")
    else:
        print("No audio frames recorded, recording failed!")

    keyboard.unhook_all()

def recognize_speech(file_path):
    if not os.path.exists("model"):
        print("Please download the model and place it in the current directory.")
        return ""

    wf = wave.open(file_path, "rb")
    if wf.getnchannels() != 1 or wf.getsampwidth() != 2 or wf.getframerate() != 16000:
        print("Unsupported audio format")
        return ""

    model = Model("model")
    rec = KaldiRecognizer(model, wf.getframerate())
    rec.SetWords(True)

    results = []
    while True:
        data = wf.readframes(4000)
        if len(data) == 0:
            break
        if rec.AcceptWaveform(data):
            results.append(rec.Result())
        else:
            results.append(rec.PartialResult())
    results.append(rec.FinalResult())

    wf.close()

    text = ""
    for result in results:
        result_dict = json.loads(result)
        if 'text' in result_dict:
            text += result_dict['text'] + " "
    return text.strip()

def compare_texts(recorded_text, standard_text):
    differences = []
    recorded_tokens = recorded_text.split()
    standard_tokens = standard_text.split()
    for i in range(min(len(recorded_tokens), len(standard_tokens))):
        if recorded_tokens[i] != standard_tokens[i]:
            differences.append((i, recorded_tokens[i], standard_tokens[i]))
    return differences

def main():
    file_path = r"D:\Project\guben\record_save"
    record_audio(file_path)
    recorded_text = recognize_speech(file_path)
    print("Recorded text:", recorded_text)
    differences = compare_texts(recorded_text, standard_text)
    if differences:
        print("Detected differences:")
        for diff in differences:
            print(f"Position {diff[0] + 1}: Recorded text '{diff[1]}' differs from standard text '{diff[2]}'")
    else:
        print("Recorded text matches the standard text")

if __name__ == "__main__":
    main()

I’m new here and don’t know much about the format. If there is anything inappropriate, please let me know. Thank you.

What is the full traceback? What are the relevant variables at that point in time?

Traceback (most recent call last):
File “D:\Project\guben\yunxing2.py”, line 129, in
main()
File “D:\Project\guben\yunxing2.py”, line 118, in main
recorded_text = recognize_speech(file_path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\Project\guben\yunxing2.py”, line 75, in recognize_speech
wf = wave.open(file_path, “rb”)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\Anaconda\envs\myenv\Lib\wave.py”, line 649, in open
return Wave_read(f)
^^^^^^^^^^^^
File “D:\Anaconda\envs\myenv\Lib\wave.py”, line 282, in init
f = builtins.open(f, ‘rb’)
^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: ‘D:\Project\guben\record_save’

So the problem or that you are trying to open a folder as it is were a file, which didn’t work. On windows this produces a Permission Error instead of something more helpful.

1 Like

:open_mouth: I will try, thanks