- 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 to write in Python?
To open files in just write mode, specify 'w' as the mode. For example,
f = open('my_file.txt', 'w') f.write('Hello World') f.close()
Above code opens my_file.txt in write mode and rewrites the file to contain "Hello World". A safer approach would be using the with open syntax to avoid file from not closeing in case of an exception:
with open('my_file.txt', 'r') as f: f.write('Hello World')
- Related Articles
- How to open a file in read and write mode with Python?
- How to open a binary file in read and write mode with Python?
- How to open a file just to read in 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 get the current open file line in Python?
- How to open a file in the same directory as a Python script?
- How to open a plain text file in C#?
- How to open a plain text file in Java?
- How to write binary data to a file using Python?
- Python - How to write pandas dataframe to a CSV file
- How to write a text file in Selenium with python?
- How to Write a List Content to a File using Python?
- How to open hidden file using C#?

Advertisements