简体   繁体   中英

AttributeError: 'NoneType' object has no attribute 'upper' when fetching Reddit darkmode

Im trying to fetch Reddit's dark mode but resulting in this error in the title, here's my current code;

if getenv("THEME").upper() == "DARK":
            cookie_file = open('./video_creation/data/cookie.json')
            cookies = json.load(cookie_file)
            context.add_cookies(cookies)

As noted in comments, the return value of getenv("THEME") is clearly None , upon which you cannot call upper . You should investigate why getenv("THEME") is not returning the result you expect.

If this is os.getenv you can also provide a default value.

getenv("THEME", default='light').upper()

The getenv() function is returning a None value, probably because of not being able to find the variable or something likewise. You can also try to use a try-except block to resolve to an answer incase of a TypeError. Also, if this is the getenv from the os module then you can provide a default value as mentioned above.

The try block could be done as:

try:
    if getenv("THEME").upper() == "DARK":
            cookie_file = open('./video_creation/data/cookie.json')
            cookies = json.load(cookie_file)
            context.add_cookies(cookies)
except TypeError:
    # Do something if variable is not found or something like that
    smth_default()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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