You opened the file for writing by specifying the mode as 'w'
; open the file for reading instead:
open(path, 'r')
'r'
is the default, so it can be omitted. If you need to both read and write, use the +
mode:
open(path, 'w+')
w+
opens the file for writing (truncates it to 0 bytes) but also lets you read from it. If you use r+
it is also opened for both reading and writing, but won’t be truncated.
If you are to use a dual-mode such as r+
or w+
, you need to familiarize yourself with the .seek()
method too, as using both reading and writing operations will move the current position in the file and you’ll most likely want to move that current file position explicitly between such operations.
See the documentation of the open()
function for further details.
Related Posts:
- How to open a file using the open with statement
- Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory
- Writing a list to a file with Python
- Writing a list to a file with Python
- How to open a file for both reading and writing?
- How do I copy a file in Python?
- How do I copy a file in Python?
- Using “with open() as file” method, how to write more than once? [duplicate]
- What is the difference between rw+ and r+
- Correct way to write line to file?
- How to read a file line-by-line into a list?
- TypeError: a bytes-like object is required, not ‘str’ when writing to a file in Python3
- Confused by python file mode “w+”
- Print string to text file
- File open and close in python
- Do I understand os.walk right?
- FileNotFoundError: [Errno 2] No such file or directory [duplicate]
- How to move a file in Python?
- FileNotFoundError: [Errno 2] No such file or directory
- How to move a file in Python?
- How do you append to a file?
- ValueError : I/O operation on closed file
- Why do I get “Pickle – EOFError: Ran out of input” reading an empty file?
- write() versus writelines() and concatenated strings
- How do I copy a file in Python?
- How do I check whether a file exists without exceptions?
- Call a function from another file?
- How can I check file size in Python?
- Python list directory, subdirectory, and files
- Where does this come from: -*- coding: utf-8 -*-
- Open file in a relative location in Python
- Why am I getting a FileNotFoundError?
- Python: Write array values into file
- Importing variables from another file?
- How to copy files?
- Flask raises TemplateNotFound error even though template file exists
- importing external “.txt” file in python
- Search and replace a line in a file in Python
- How to save a dictionary to a file?
- How do you read a file into a list in Python?
- SSL error : routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
- What is the perfect counterpart in Python for “while not EOF”
- .write not working in Python
- Creating files and directories via Python
- How to convert a file into a dictionary?
- How to delete a specific line in a file?
- Python read in string from file and split it into values
- How often does python flush to a file?
- How to open every file in a folder
- TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper
- Loading and parsing a JSON file with multiple JSON objects
- What is an easy way to implement fprintf in python?
- How to find out whether a file is at its `eof`?
- How to append new data onto a new line
- Find all files in a directory with extension .txt in Python
- How can I parse a YAML file in Python
- Cannot find module cv2 when using OpenCV
- Maximum and Minimum values for ints
- How is Anaconda related to Python?
- What is the python keyword “with” used for? [duplicate]
- What is the purpose of meshgrid in Python / NumPy?
- How to add to the PYTHONPATH in Windows, so it finds my modules/packages?
- FileNotFoundError: [WinError 2] The system cannot find the file specified:
- SyntaxError: non-default argument follows default argument
- socket.error: [Errno 48] Address already in use
- Creating a basic auto clicker in python
- What’s the difference between torch.stack() and torch.cat() functions?
- proper name for python * operator?
- How to disable Python warnings?
- LinAlgError: Last 2 dimensions of the array must be square
- How to get data from SNMP with python?
- What does axis in pandas mean?
- Dynamically growing a python array when assigning to it
- “OverflowError: Python int too large to convert to C long” on windows but not mac
- Displaying better error message than “No JSON object could be decoded”
- List attributes of an object [duplicate]
- How can I set the aspect ratio in matplotlib?
- In Python how should I test if a variable is None, True or False
- Pythonic way to iterate through a range starting at 1
- Convert from CSV to array in Python
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- ValueError: unsupported pickle protocol: 3, python2 pickle can not load the file dumped by python 3 pickle?
- Relative paths in Python
- Convert bytes to int?
- How do I maximize the display screen in PyGame?
- Why does PyQt crashes without information? (exit code 0xC0000409)
- Python: count repeated elements in the list
- Matplotlib: TypeError: can’t multiply sequence by non-int of type ‘numpy.float64’
- NameError: global name ‘unicode’ is not defined – in Python 3
- Create nice column output in python
- TypeError: unsupported operand type(s) for +=: ‘int’ and ‘list’
- TypeError: int() argument must be a string, a bytes-like object or a number, not ‘list’
- Histogram plotting “AttributeError: max must be larger than min in range parameter.”
- Python TypeError: non-empty format string passed to object.__format__
- AttributeError: ‘Series’ object has no attribute ‘reshape’
- TemplateDoesNotExist at /
- How do I update a Python package?
- Selenium “Unable to find a matching set of capabilities” despite driver being in /usr/local/bin
- How to print a specific row of a pandas DataFrame?
- AttributeError: ‘Series’ object has no attribute ‘split’ error in sending emails