🌐
DataCamp
datacamp.com › tutorial › functions-python-tutorial
Python Functions: How to Call & Write Functions | DataCamp
November 22, 2024 - To define a function in Python, use the def keyword, name your function, add optional parameters in parentheses, write your code, and optionally return a value.
🌐
Earth Data Science
earthdatascience.org › home
Write Functions in Python | Earth Data Science - Earth Lab
September 3, 2020 - Then, you can write the return statement to return the new value inches. # Convert input from mm to inches def mm_to_in(mm): inches = mm / 25.4 return inches · The function above is complete regarding code. However, as previously discussed, good documentation can help you and others to easily use and adapt this function as needed. Python promotes the use of docstrings for documenting functions.
🌐
W3Schools
w3schools.com › python › ref_file_write.asp
Python File write() Method
Python Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary
🌐
Tutorialspoint
tutorialspoint.com › python › file_write.htm
Python File write() Method
The Python File write() method writes a string to a file. When the method writes the string into this file, it is first written into the internal buffer; and once this buffer is full, the contents are then transferred to the current file.
🌐
Python documentation
docs.python.org › 3 › tutorial › inputoutput.html
7. Input and Output — Python 3.14.4 documentation
So far we’ve encountered two ways of writing values: expression statements and the print() function. (A third way is using the write() method of file objects; the standard output file can be referenced as sys.stdout.
🌐
Medium
jessica-miles.medium.com › writing-functions-in-python-a-beginners-guide-ed9182db959b
Writing Functions in Python— A Beginner’s Guide | by Jessica Miles | Medium
November 1, 2021 - Write the function definition using the format def function_name(): with empty parentheses (no parameters) to start with. Make sure to put a colon at the end. It’s conventional in Python to use all lower-case letters for function names, and ...
🌐
Python Beginners
python-adv-web-apps.readthedocs.io › en › latest › functions.html
Functions in Python — Python Beginners documentation
You can write and save Python files in Atom or in Mu. To run a script named foobar.py that’s in the current directory, type this at the bash prompt ($) in Mac or the Windows Command Prompt: ... You do not need to be in your virtualenv to do this, but it’s okay if you are. Sweigart notes that many functions operate as “black boxes”: This describes a function with parameters (it takes arguments) and a return statement.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-functions
Python Functions - GeeksforGeeks
Mutable objects: Changes inside the function affect the original object. Immutable objects: The original value remains unchanged. ... def myFun(x): x[0] = 20 lst = [10, 11, 12, 13] myFun(lst) print(lst) def myFun2(x): x = 20 a = 10 myFun2(a) print(a) ... Note: Python always passes objects by reference.
Published   2 weeks ago
Find elsewhere
🌐
W3Schools
w3schools.com › python › python_functions.asp
Python Functions
The code inside the function must be indented. Python uses indentation to define code blocks. To call a function, write its name followed by parentheses:
🌐
Medium
medium.com › python-tutorial-beginner-to-advance › how-to-write-functions-in-python-fbf6480c66bb
How to write Functions in Python? | by Haider Ali | Python Tutorial [Beginner to Advance] | Medium
October 6, 2025 - How to write Functions in Python? In this tutorial you will learn functions in Python, passing arguments to functions and scope of variables. What is a Function? A block of code is combined to create …
🌐
Real Python
realpython.com › defining-your-own-python-function
Defining Your Own Python Function – Real Python
June 11, 2025 - Learn how to define your own Python function, pass data into it, and return results to write clean, reusable code in your programs.
🌐
DEV Community
dev.to › emmyjaff › how-to-write-functions-in-python-4anh
How to Write Functions In Python - DEV Community
December 31, 2023 - The syntax for defining a function ... defining “mu_function”. To call the function, we’d have to write out the function with a parenthesis after its name....
🌐
Career Karma
careerkarma.com › blog › python › how to write python functions
How to Write Python Functions | Career Karma
December 1, 2023 - This tutorial discussed the basics of functions in Python, how to write and call a function, and how to work with arguments and parameters.
🌐
KDnuggets
kdnuggets.com › 5-tips-for-writing-better-python-functions
5 Tips for Writing Better Python Functions - KDnuggets
June 4, 2024 - It's quite common to write Python functions that generate sequences such as a list of values. But as much as possible, you should avoid returning lists from Python functions. Instead you can rewrite them as generator functions. Generators use lazy evaluation; so they yield elements of the sequence on demand rather than computing all the values ahead of time.
🌐
Khuyentran1401
khuyentran1401.github.io › reproducible-data-science › structure_project › functions.html
2.2. Best Practices for Writing Python Functions — Build a Reproducible and Maintainable Data Science Project
Users can understand what the function extract_texts_from_multiple_files does by looking at its name. Don’t be afraid to write long names. It is better to write long names rather than write vague names.
🌐
TutorialBrain
tutorialbrain.com › home › python file write
Python File Write — TutorialBrain
July 10, 2025 - Prev Next Python File Write – write() method The write() method is used to write into a file using the file object. This is an inbuilt method of Python. write() method writes strings into the file.
🌐
Dive into Python
diveintopython.org › home › functions & methods › file methods › write()
write() in Python - File Methods with Examples
The write() function is used in Python as part of the File Methods. It is used to write data to a file in a specified format. This function takes a string as input and writes it to the file. If the file does not exist, it will be created.
🌐
freeCodeCamp
freecodecamp.org › news › python-functions-define-and-call-a-function
Python Functions – How to Define and Call a Function
March 16, 2022 - In Python, you define a function with the def keyword, then write the function identifier (name) followed by parentheses and a colon.
Top answer
1 of 4
7

You must append to the file, rather than overwrite it.

According to Python Pocket Reference (Mark Lutz, O'rielly):

Mode is an optional string that specifies the mode in which the file is opened. It defaults to 'r' which means open for reading in text mode. Other common values are 'w' for writing (truncating the file if it already exists) and 'a' for appending.

Here are the modes for open(): (according to Tutorials Point)

r - Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.

rb - Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.

r+ - Opens a file for both reading and writing. The file pointer will be at the beginning of the file.

rb+ - Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file.

w - Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

wb - Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

w+ - Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

wb+ - Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

a - Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

ab - Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

a+ - Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

ab+ - Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

Also, 'w' and 'a' will create a file if it does not exist, while 'r' will throw an exception of type IOError.

2 of 4
4

Using "a" (append) mode on your open() call as others suggest will solve your immediate problem, but then you have to worry about how to clear out the file for each run, or create it if it doesn't exist, etc. You can do this by using "w" mode for the first function and "a" for subsequent ones, but this introduces a situation where they must be called in the right order. Which could come back to bite you in six months when you want them in a different order and have to chase down why only some of them are showing up in the file (because the one with "w" as the mode is now in the middle).

A better solution is to open the file outside the function and pass it in as an argument (perhaps using a with statement around the function calls so it gets closed automatically after). That not only avoids the need to worry about the mode in each of your functions, it avoids repeatedly opening and closing the file, which is an inefficiency (albeit a minor one on modern systems):

def SimulationFunction1(args, outfile):
    # some simulation, which results in simOUT
    outfile.write(str(simOUT) + '\n')

with open("my_output_file.txt", "w") as out:
    SimulationFunction1(args, outfile=out)
    SimulationFunction2(args, outfile=out)
    SimulationFunction3(args, outfile=out)

Or better yet, just have the functions return the result and have the caller deal with writing them:

def SimulationFunction1(args):
    # some simulation, which results in simOUT
    return str(simOUT)

with open("my_output_file.txt", "w") as out:
    out.write(SimulationFunction1(args) + `\n`)
    out.write(SimulationFunction2(args) + `\n`)
    out.write(SimulationFunction3(args) + `\n`)

This latter approach illustrates separation of concerns. Each part of your code should be concerned with doing one thing: calculating a value, writing to a file, or whatever. Doing multiple things in a single "chunk" of code (function, class, whatever) makes it harder to understand, to maintain, and to reuse.

There's a place where I could have (in fact, probably should have) separated concerns there, and didn't... can you see it? :-)