🌐
W3Schools
w3schools.com › python › ref_func_open.asp
Python open() Function
Python Examples Python Compiler ... Python Interview Q&A Python Bootcamp Python Training ... The open() function opens a file, and returns it as a file object....
🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.5rc1 documentation
February 27, 2026 - The default mode is 'r' (open for reading text, a synonym of 'rt'). Modes 'w+' and 'w+b' open and truncate the file. Modes 'r+' and 'r+b' open the file with no truncation. As mentioned in the Overview, Python distinguishes between binary and ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-open-function
Python open() Function - GeeksforGeeks
July 23, 2025 - The Python open() function is used to open internally stored files.
🌐
Real Python
realpython.com › ref › builtin-functions › open
open() | Python’s Built-in Functions – Real Python
The built-in open() function in Python is used to open a file and return a corresponding file object. This function allows you to read from or write to files, with various options for file modes (e.g. text/binary) and encoding.
🌐
Programiz
programiz.com › python-programming › methods › built-in › open
Python open()
The open() function returns a file object which can used to read, write and modify the file.
🌐
Python Tips
book.pythontips.com › en › latest › open_function.html
23. open Function — Python Tips 0.1 documentation
Unfortunately, open does not allow explicit encoding specification in Python 2.x. However, the function io.open is available in both Python 2.x and 3.x (where it is an alias of open), and does the right thing. You can pass in the encoding with the encoding keyword.
🌐
freeCodeCamp
freecodecamp.org › news › with-open-in-python-with-statement-syntax-example
With Open in Python – With Statement Syntax Example
August 3, 2024 - So, the open() function does what the name implies – it opens a file for you so you can work with the file.
Find elsewhere
🌐
Flexiple
flexiple.com › python › open-python
How to use Open() in Python? | Flexiple Tutorials | Python - Flexiple
The open() function in Python lets you open any internal file if it exists which can be read, written, or appended according to the user's need. The different string values in mode let you perform those functions as per your need.
🌐
Codecademy
codecademy.com › docs › python › built-in functions › open()
Python | Built-in Functions | open() | Codecademy
April 10, 2025 - The open() function in Python opens a file and returns it as a file object. It provides access to the file system, allowing programs to read from or write to files.
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › functions › open.html
open — Python Reference (The Right Way) 0.1 documentation
Python provides many file handling modules including fileinput, os, os.path, tempfile, and shutil. >>> open('C:\\alice_in_wonderland.txt') <open file 'C:\\alice_in_wonderland.txt', mode 'r' at 0x02C1C390> >>> alice = open(r'C:\alice_in_wonderland.txt', 'r+') >>> alice.readline() " ALICE'S ADVENTURES IN WONDERLAND\n"
🌐
W3Schools
w3schools.com › python › python_file_open.asp
Python File Open
Python Examples Python Compiler ... demofile.txt This file is for testing purposes. Good Luck! To open the file, use the built-in open() function....
Top answer
1 of 5
402

Python allows putting multiple open() statements in a single with. You comma-separate them. Your code would then be:

def filter(txt, oldfile, newfile):
    '''\
    Read a list of names from a file line by line into an output file.
    If a line begins with a particular name, insert a string of text
    after the name before appending the line to the output file.
    '''

    with open(newfile, 'w') as outfile, open(oldfile, 'r', encoding='utf-8') as infile:
        for line in infile:
            if line.startswith(txt):
                line = line[0:len(txt)] + ' - Truly a great person!\n'
            outfile.write(line)

# input the name you want to check against
text = input('Please enter the name of a great person: ')    
letsgo = filter(text,'Spanish', 'Spanish2')

And no, you don't gain anything by putting an explicit return at the end of your function. You can use return to exit early, but you had it at the end, and the function will exit without it. (Of course with functions that return a value, you use the return to specify the value to return.)

Using multiple open() items with with was not supported in Python 2.5 when the with statement was introduced, or in Python 2.6, but it is supported in Python 2.7 and Python 3.1 or newer.

http://docs.python.org/reference/compound_stmts.html#the-with-statement http://docs.python.org/release/3.1/reference/compound_stmts.html#the-with-statement

If you are writing code that must run in Python 2.5, 2.6 or 3.0, nest the with statements as the other answers suggested or use contextlib.nested.

2 of 5
40

Use nested blocks like this,

with open(newfile, 'w') as outfile:
    with open(oldfile, 'r', encoding='utf-8') as infile:
        # your logic goes right here
🌐
Tutorialspoint
tutorialspoint.com › python › python_open_function.htm
Python open() Function
The Python open() function is a built-in function that is used to open a file and return its corresponding file object. To perform file operations such as reading and writing, you first need to open the file using the open() function.
🌐
Tutorialspoint
tutorialspoint.com › python › os_open.htm
Python os.open() Method
Python method os.open() opens the specified file and returns a corresponding file descriptor. It also allows us to set various flags and modes to files. Always remember the default mode is 0o0777 (octal), which set the file permissions to read,
🌐
Python-future
python-future.org › open_function.html
open() — Python-Future documentation
The Python 3 builtin open() function for opening files returns file contents as (unicode) strings unless the binary (b) flag is passed, as in:
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-read-file-open-write-delete-copy
Python open() Function Explained: How to Open, Read, and Write Files | DigitalOcean
June 25, 2025 - By the end of this tutorial, you’ll ... tasks in your projects. ... The with open() statement is the standard and safest method for handling files because it automatically closes the file for you, even if your code runs into an error....
🌐
AskPython
askpython.com › home › how to open files in python
How to Open Files in Python - AskPython
February 16, 2023 - You should now have a grasp on how to open a file in Python and handle the different modes for opening a file with the open() method.
🌐
How-To Geek
howtogeek.com › home › programming › 8 ways to use the python open() function
8 Ways to Use the Python open() Function
October 5, 2025 - The open() function has quite a complicated signature, but in the simplest cases, you can use it to open a text file like this: ... By default, Python opens this file in read mode, meaning that you can only read from it.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › python file open: how to open a file in python?
Python File Open: How to Open a File in Python?
August 15, 2024 - To open the file, we used the ‘open()’ function by providing the file path and storing the content in another variable ‘file_content’. Finally, we printed the content using the Print statement of Python and closed the file using the ‘close()’ function.