Navin
Navin

Reputation: 57

Check file extension of a non string

from tkinter.filedialog import askopenfilename
import os

load_list = open(askopenfilename(), "rb")
file_name, file_extension = os.path.splitext(load_list)
if str(file_extension).lower() == (".p", ".pickle"):
    print("pickle")

I've read that this method works with the file path given as a string however this does not work with my method of getting the file path.

I am receiving an error:

AttributeError: '_io.BufferedReader' object has no attribute 'rfind'

Upvotes: 2

Views: 201

Answers (1)

alecxe
alecxe

Reputation: 473813

You are comparing a string with a tuple. You've probably meant to use in:

if file_extension.lower() in (".p", ".pickle"):

And, you should pass a filename to splitext(), not the file object. Replace:

file_name, file_extension = os.path.splitext(load_list)

with:

filename = askopenfilename()
_, file_extension = os.path.splitext(filename)

Upvotes: 2

Related Questions