-2

I am working on an app that will be something like a text editor, using kivy. I included a FileChooser to choose a file to edit, I included a try except to catch the problems of files readability like videos and executable files.The problem is that this doesn't work and it raises an error of decode so the try except didn't work as expected. I am thinking now of something like precising the extentions of files to open.But I would like to request from you a list of extensions that python can read. Please Help I think no need to share my code because there is nothing specific in it and I think my request is clear:

A list of extensions that python can read. Ok so here is my check function that is called when a user click a button after choosing a file(This is in kivy by the way):

def check(self):
            app= App.get_running_app()
            with open(os.path.join(sys.path[0], "data.txt"), "r") as f:
                dataa= f.read()
                data= dataa.split('\n')
            if self.namee.text in dataa:
                for i in data:
                    ii = i.split(':')
                    if ii[1] == self.namee.text and ii[3]== self.password.text:
                        self.btn.text="Identified successfully!!"
                        time.sleep(0.5)
                        break
                    else:
                        self.btn.text="Not identified successfully!!"
            elif self.password.text.strip() =="":
                try: 
                    with codecs.open(self.namee.text, 'r', encoding="utf-8", errors='ignore') as file:
                        try:
                            test = file.read()
                            print(test)
                            self.btn.text=="Identified successfully!!"
                        except:
                            self.btn.text ="Oops! Couldn't access the content of this file, try again later or verify your typing."
                    self.btn.text=="Identified successfully!!"
                except:
                    self.btn.text ="Oops! Couldn't open the file, try again later or verify your typing."

            else:
                self.btn.text = "Cannot find any secret file with this name!"
            if self.btn.text=="Identified successfully!!":
                    app.root.current = "control"
                    self.btn.text = "open"
                    self.namee.text = ""
                    self.password.text = ""
Taha LYOUSFI
  • 21
  • 1
  • 6

2 Answers2

1

File extensions don't actually really work that way, there is no simple list that Python can read, and nor does the extension actually mean anything - for instance, you could give a video file the extension .py if you wanted.

You initial idea of catching errors is the right way to do things, and you'll want to have that functionality even if you also limit the available file extensions, since a file with a valid extension may contain invalid data. There's no reason it shouldn't work so probably you had some bug you can resolve.

Also, your request isn't clear, since "how to limit the type of files to open in kivy" != "what extensions can python read". It sounds like you already know the answer to the former question.

inclement
  • 29,124
  • 4
  • 48
  • 60
1

To my understanding python can read any file. Extensions don't matter. What matters is the way you handle the file.

Albert Alberto
  • 801
  • 8
  • 15