- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 write a single line in text file using Python?
You can simply write the line to file using write function.
f = open('myfile', 'w') f.write('hi there\n') # python will convert \n to os.linesep f.close() # you can omit in most cases as the destructor will call it
Alternatively, you can use the print() function which is available since Python 2.6+
from __future__ import print_function # Only needed for Python 2 print("hi there", file=f)
For Python 3 you don't need the import, since the print() function is the default.
- How to read complete text file line by line using Python?
- How to write into a file from command line using Python?
- How to write multiple lines in text file using Python?
- How to read an entire line from a text file using Python?
- How to write a text file in Selenium with python?
- How to write text and output it as a text file using R?
- How to write single line comment in Java?
- How to write single-line comments in C#?
- How to write binary data to a file using Python?
- How to read a file from command line using Python?
- How to search and replace text in a file using Python?
- How to read a text file in Python?
- How to open a file to write in Python?
- How to find and replace within a text file using Python?
- How to encapsulate Python modules in a single file?