
👉Tutorial-2: Python Check if File Exists
👉Tutorial-3: Python Copy File Methods
👉Tutorial-4: Python File and Directory Renaming
👉Tutorial-5: Python ZIP File Creation Guide with Examples
👉Tutorial-6: Python Exception Handling
👉Tutorial-7: Python readline() Method Explained
👉Complete Guide to Creating and Managing Text Files in Python
How to Create and Manage Text Files: Python makes file handling simple with its built-in functions. Whether you want to create, write, append, or read text files, Python offers intuitive methods to achieve these tasks efficiently.
👉How to Create and Manage Text Files
How to Create and Manage Text Files: Creating a text file in Python is easy using the open() function. Here’s the syntax:
python
file_object = open(“filename”, “mode”)
- filename – Name of the file you want to create or open.
- mode – Specifies the mode for file handling (e.g., read, write, append).
👉Steps to Create a Text File in Python
How to Create and Manage Text Files
Open the File
python
f = open(“Software_Moji_Moji.txt”, “w+”)
-
- “w+” mode opens the file for both writing and reading.
- If the file doesn’t exist, it creates a new one.
- If the file already exists, it overwrites the content.
Write Data into the File
python
for i in range(10):
f.write(“This is line %d\r\n” % (i + 1))
-
- The write() method adds content to the file.
- %d inserts dynamic values in the text, like line numbers.
Close the File
python
f.close()
-
- Closing the file ensures the data is saved properly.
Output:
arduino
This is line 1
This is line 2
…
This is line 10
👉How to Append Data to a Text File in Python
How to Create and Manage Text Files: To add new data to an existing file, use “a+” mode.
👉Steps to Append Data
Open the File in Append Mode
python
f = open(“Software_Moji_Moji.txt”, “a+”)
Write Additional Data
python
for i in range(2):
f.write(“Appended line %d\r\n” % (i + 1))
Close the File
python
f.close()
Output (After Appending):
arduino
This is line 1
This is line 2
…
This is line 10
Appended line 1
Appended line 2
👉How to Read a Text File in Python
To read the content of a file, use “r” mode.
👉Steps to Read a File
Open the File in Read Mode
python
f = open(“Software_Moji_Moji.txt”, “r”)
Check if File is in Read Mode
python
if f.mode == ‘r’:
contents = f.read()
print(contents)
Close the File
python
f.close()
👉Reading a File Line by Line
If the file content is large, reading it line by line is efficient.
python
f = open(“Software_Moji_Moji.txt”, “r”)
lines = f.readlines()
for line in lines:
print(line.strip()) # Removes extra spaces or newlines
👉Python File Modes
Mode | Description |
‘r’ | Read-only mode (default). |
‘w’ | Write mode (overwrites the file if it exists). |
‘x’ | Create a new file (fails if file exists). |
‘a’ | Append mode (adds new content without deleting old data). |
‘t’ | Text mode (default). |
‘b’ | Binary mode. |
‘+’ | Open file for reading and writing. |
👉Complete Python Code Example for File Handling
👉Python 3 Code Example
python
def main():
# Create and Write to File
f = open(“Software_Moji_Moji.txt”, “w+”)
for i in range(10):
f.write(“This is line %d\r\n” % (i + 1))
f.close()
# Append Data to File
f = open(“Software_Moji_Moji.txt”, “a+”)
for i in range(2):
f.write(“Appended line %d\r\n” % (i + 1))
f.close()
# Read File Data
f = open(“Software_Moji_Moji.txt”, “r”)
if f.mode == ‘r’:
contents = f.read()
print(contents)
f.close()
if __name__ == “__main__”:
main()
👉How to Create and Manage Text Files Summary
✅ Use open(“filename”, “w+”) for creating and writing text files.
✅ Use open(“filename”, “a+”) for appending data to an existing file.
✅ Use read() or readlines() to read file content efficiently.