0% found this document useful (0 votes)
11 views3 pages

Python File Handling Commands Guide

This document provides a comprehensive guide on Python file input/output (I/O) commands, including how to open, write, read, append, and delete files. It emphasizes best practices, such as using 'with open' for file handling, and includes examples for each command. Additionally, it covers checking for file existence and creating directories and files.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Python File Handling Commands Guide

This document provides a comprehensive guide on Python file input/output (I/O) commands, including how to open, write, read, append, and delete files. It emphasizes best practices, such as using 'with open' for file handling, and includes examples for each command. Additionally, it covers checking for file existence and creating directories and files.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python File Input/Output (I/O) - All Commands

PYTHON FILE INPUT/OUTPUT (I/O) - ALL COMMANDS

1. OPENING A FILE

Syntax:
file = open("[Link]", "mode")

Modes:
'r' : Read (default)
'w' : Write (creates file or overwrites)
'a' : Append
'x' : Create (fails if file exists)
'b' : Binary mode
't' : Text mode (default)
'r+' : Read and Write

Example:
file = open("[Link]", "w")

2. WRITING TO A FILE

Syntax:
[Link]("your text")

Example:
file = open("[Link]", "w")
[Link]("Welcome to Python!")
[Link]()

3. READING FROM A FILE

Methods:
[Link](size) Read 'size' characters
[Link]() Read one line
[Link]() Read all lines (as list)

Example:
file = open("[Link]", "r")
content = [Link]()
print(content)
[Link]()

4. APPENDING TO A FILE

Example:
Python File Input/Output (I/O) - All Commands

file = open("[Link]", "a")


[Link]("\nThis is an appended line.")
[Link]()

5. USING "WITH OPEN" (BEST PRACTICE)

Syntax:
with open("filename", "mode") as file:
# do something

Example:
with open("[Link]", "r") as file:
content = [Link]()
print(content)

6. READING FILE LINE BY LINE

Example:
with open("[Link]", "r") as file:
for line in file:
print([Link]())

7. FILE OBJECT METHODS

[Link](size)
[Link]()
[Link]()
[Link](text)
[Link](list_of_strings)
[Link](offset)
[Link]()
[Link]()

8. CHECK IF FILE EXISTS

import os
if [Link]("[Link]"):
print("File exists")
else:
print("File not found")

9. DELETE A FILE

import os
[Link]("[Link]")
Python File Input/Output (I/O) - All Commands

10. CREATE DIRECTORY & FILE

import os
[Link]("myfolder") # create folder
open("myfolder/[Link]", "w") # create file inside folder

SUMMARY (QUICK COMMANDS)

Open File open("[Link]", "r")


Write File [Link]("text")
Read File [Link]() / [Link]()
Append File open("[Link]", "a")
Close File [Link]()
With Block with open(...) as file:
Delete File [Link]("[Link]")
Check Exists [Link]("[Link]")

You might also like