- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to open a file in read and write mode with Python?
To open files in read/write mode, specify 'w+' as the mode. For example,
f = open('my_file.txt', 'w+') file_content = f.read() f.write('Hello World') f.close()
Above code opens my_file.txt in write mode, stores the file content in file_content variable and rewrites the file to contain "Hello World". You can also use r+ mode as it doesn't truncate the file.
- Related Articles
- How to open a binary file in read and write mode with Python?
- How to open a file in binary mode with Python?
- How to open a file in append mode with Python?
- How to open a binary file in append mode with Python?
- How to open a file just to read in python?
- How to open a file to write in Python?
- How to set read and write position in a file in Python?
- How to read and write a file using Javascript?
- How to read a text file in Selenium with python?
- How to use seek() method to reset a file read/write position in Python?
- Read and Write to an excel file using Python openpyxl module
- How to read a text file in Python?
- How to read CSV file in Python?
- How to read JSON file in Python
- How to write a text file in Selenium with python?

Advertisements