Python Forum
TypeError: 'NoneType' object is not subscriptable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: 'NoneType' object is not subscriptable
I'm trying to check whether the user entered the correct path or not.
when the output appears it doesn't give any error but when i close the output it gets an error :

Traceback (most recent call last):
  File "", line 115, in <module>
  File "", line 88, in main
    if check_file(values["Path"]):
TypeError: 'NoneType' object is not subscriptable
I think it's wrong the
but when I try to catch the error it still returns the same error

Here's the code to try to catch the error :
def check_file(path):
        return True
    except TypeError:
        return False
and this is the main code :
import os
import pyttsx3
import requests
import googletrans
import PySimpleGUI as sg
import speech_recognition as sr

def audio_layout():
    """ return audio layout """
    layout = [
        [sg.In(key="Path", size=(39, 0))],
            sg.FileBrowse("Browse", size=(10, 0), target="Path"),
            sg.Button("Text", size=(10, 0)),
            sg.Button("Voice", size=(10, 0)),

    return sg.Window("Voice Translate", layout, finalize=True)

def result_layout():
    """ return result layout """
    layout = [[sg.Output(size=(60, 20))]]

    return sg.Window("Voice Translate", layout, finalize=True)

def language_translate(text, lang="id"):
    """ returns the text after it is translated """
    translator = googletrans.Translator()
    lang_convert = translator.translate(text, dest=lang)

    return lang_convert.text

def audio_to_text(audio):
    """ convert audio to text """
    recognizer = sr.Recognizer()
    sound = sr.AudioFile(audio)
    with sound as source:
        audio = recognizer.record(source)

    return recognizer.recognize_google(audio)

def text_to_audio(text):
    """ convert text into voice """
    engine = pyttsx3.init()

def check_internet():
    """ return true if internet is connect  """
    # google url only to test whether the user is connected to the internet
    url = ""
    timeout = 3
        requests.head(url, timeout=timeout)
        return True
    except requests.ConnectionError:
        sg.popup("Lost Connection!")
        return False

def check_file(path):
        return True
    except TypeError:
        return False

# main program
def main():
    audio_window, result_window, allowed = audio_layout(), None, False
    while check_internet():
        # read/get all event in all window
        window, event, values = sg.read_all_windows()
        # audio layout/window event handling
        if window == audio_window and event in (sg.WIN_CLOSED, "Exit"):

        # bug! "TypeError: 'NoneType' object is not subscriptable"
        if check_file(values["Path"]):
            convert_to_text = audio_to_text(values["Path"])
            text_translate = language_translate(convert_to_text)
            allowed = True

        # user get the translate result of their voice
        if event == "Text" and allowed:
            result_window = result_layout()
            # show the output on output widget

        if event == "Voice" and allowed:

        # result layout/window event handling
        if window == result_window and event in (sg.WIN_CLOSED, "Exit"):
            result_window = None
            allowed = False

    if result_window is not None:

if __name__ == "__main__":
I know I can put the try and except statements above if or even replace them but I think it becomes unclean code and might make my code dirty
Please show the complete error message. It surely tells on what line the error occurs.
The error is self-explanatory. You are trying to index None. You can not, because 'NoneType' object is not subscriptable. This means that you tried to do:


In general, the error means that you attempted to index an object that doesn't have that functionality. You might have noticed that the method sort() that only modify the list have no return value printed – they return the default None. This is a design principle for all mutable data structures in Python.
buran write Nov-17-2021, 05:45 AM:
Clikcbait link removed

Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 211 Jan-02-2022, 09:53 AM
Last Post: snippsat
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 172 Jan-01-2022, 04:16 PM
Last Post: MaartenRo
  'int' object is not subscriptable after API call ed8484 1 530 Sep-18-2021, 02:06 PM
Last Post: ed8484
  TypeError: 'DataFrame' object is not callable using Pandas in Python sofiavlachou 1 2,721 Sep-02-2021, 03:24 PM
Last Post: buran
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 1,082 Aug-17-2021, 05:24 PM
Last Post: Franky77
  Message='int' object is not subscriptable DanielCook 4 732 Aug-10-2021, 05:21 PM
Last Post: deanhystad
  python 3: TypeError: a bytes-like object is required, not 'str' wardancer84 3 1,056 Jul-09-2021, 05:55 PM
Last Post: deanhystad
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 1,922 May-02-2021, 03:45 PM
Last Post: Anldra12
  Bool Object is not Subscriptable quest 1 1,265 May-02-2021, 11:12 AM
Last Post: Yoriz
  Float Object is not Subscriptable quest 2 1,138 Apr-20-2021, 09:28 AM
Last Post: quest

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020