-2

I am using this in Python 3.6:

filetest="test.txt"
with open(filetest,"r") as file:

How should I make Python console to display "File not available" as an output if no input on the filetest is detected? (such as there is no test.txt in the directory)

Tony Chou
  • 584
  • 1
  • 9
  • 26
Nytro
  • 143
  • 1
  • 12
  • 2
    Put a `try/except` block or check the presence of the file before hand using `os` or `path` module – bigbounty Aug 17 '20 at 05:22
  • Add try/except block and also catch `FileNotFoundError` if the file you want to read is not exists. Catch a bare exception is not a good habit for all programming language. – Tony Chou Aug 17 '20 at 05:29
  • `'test.txt'` is a string with a file name, `test.txt` is the attribute `txt` of the object `test`. – Klaus D. Aug 17 '20 at 05:43

3 Answers3

2

You can just wrap it in a try-except block, and if there is no file named "test.txt" then you will print the message: "File not available".

A simple example is:

filename="test.txt"
try:
    with open(filetest,"r") as file:
        pass
except OSError as e:
    print("File not available")
    print(e.strerror)

I added the OSError because it indicate problem in file handling where you can read about in the following link: https://docs.python.org/3/library/exceptions.html

One can also use the FileNotFoundError exception.

David
  • 8,113
  • 2
  • 17
  • 36
  • 1
    You need something in the `with` block (even just a `pass` if you don't need to use the file) to make this syntactically legal. – ShadowRanger Aug 17 '20 at 05:24
  • 1
    Blank except clauses aren’t necessarily something to encourage ... perhaps be more specific with the `except` clause ... here you’ll happily catch all sorts of other errors in the `with` block – donkopotamus Aug 17 '20 at 05:25
  • @ShadowRanger correct, originally I has a print which want added to the answer! – David Aug 17 '20 at 05:28
  • @donkopotamus You are right, added the `OSError` exception and added a link for the user to read about built-in exceptions – David Aug 17 '20 at 05:32
  • The broader exception is good. You could `except OSError as e:` and then `print(e.strerror)` so that you don't mask a different problem with an assumed no-such-file. – tdelaney Aug 17 '20 at 05:36
  • @tdelaney Correct, this is why I used the `OSError` but added the option of a more specific exception. – David Aug 17 '20 at 05:41
2

use try-except a simple sample has given below:

filename = 'test.txt'
try:
    with open(filename,"r") as file:
        read_file = file.read()

except FileNotFoundError:
    print("File not found")
Tasnuva Leeya
  • 2,515
  • 1
  • 13
  • 21
1

Try-except blocks are what you look for.

try:
    filename=test.txt
    with open(filetest,"r") as file:
        print('File is available')
except EnvironmentError: # any IOError or OSError will be captured
    print('File not available')
Alperen Kantarcı
  • 1,038
  • 9
  • 27
  • 1
    The exception clause is very “wide” ... catching exceptions that may have nothing to do with opening the file ... for example, the current code will have an error before you even attempt to open the file ... – donkopotamus Aug 17 '20 at 05:27