We use cookies to enhance your browsing experience.

Learn More
Back to Library
PYTHONpython

Read File safely

Read file content using 'with' statement.

def read_file(filename):
    try:
        with open(filename, 'r', encoding='utf-8') as f:
            return f.read()
    except FileNotFoundError:
        return None

content = read_file('data.txt')
python file io
25+Total Tools
Operations
4Languages
100%Privacy

We use cookies to enhance your experience and serve personalized ads.