Home python How to check the availability of a file?

How to check the availability of a file? [Duplicate]

Author

Date

Category

I tried two ways:

1.

f = path (file_location + '1_classes /' + self.lineedit1.text () + '.txt')
  If F.is_file ():
    self.uch ()
    File.Close ()
  ELSE:
    self.och ()
    File.Close ()

2.

try:
  File = Open (File_Location + 1_CLASSES / "+ Self.LineEdit1.Text () + '.txt')
  Except Ioerror AS E:
    self.och ()
    File.Close ()
  ELSE:
    self.uch ()
    File.Close ()

Does do not work. If necessary, I found the path to the file so:

file_location + '1_classes /' + self.lineedit1.text () + '.txt'

The path is formed correctly, verified.
Even if there is a file, the code opens the window that reports an error.


Answer 1

Use the OS.path.exist (* File_path *) Library OS
This method returns True in existence of a file. In File_Path Specify the end address of the file.

if os.path.exists (file_location + '1_classes /' + self.lineedit1.text () + '. TXT '):
  self.uch ()
  File.Close ()
ELSE:
  self.och ()
  File.Close ()

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions