File Python File Operation (With Examples)
File Python File Operation (With Examples)
(https://programiz.pro/learn/master-python?utm_source=sticky-
36% off Learn to code by doing. Try hands-on Python with Programiz PRO. Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-python?utm_source=nav-
PRO
Courses Tutorials Examples Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Learn Python Interactively
(https://programiz.pro/learn/master-python?
Skip Ad
utm_source=tutorial-sidebar-
nav&utm_campaign=programiz&utm_medium=referral)
Python Introduction
Python File I/OPython File
Python Flow Control
Operation
A file is a container in computer storage devices used for storing data.
Python Functions
Python (/python-
Exceptionprogramming/exceptions)
Exception(/python-
Handling programming/exception- Opening Files in Python
handling)
User- (/python-
In Python, we use the open() method to open files.
defined programming/user-
Exception defined-exception) To demonstrate how we open files in Python, let's suppose we have a
file named test.txt with the following content.
Python Object & Class
(https://programiz.pro/learn/master-python?utm_source=sticky-
36% off Learn to code by doing. Try hands-on Python with Programiz PRO. Claim Discount Now # open file in current directory
banner&utm_campaign=programiz&utm_medium=referral)
file1 = open("test.txt")
Try Programiz (https://programiz.pro/learn/master-python?utm_source=nav-
PRO
Courses Tutorials Examples Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
Here,www.domain-name.com
we have created a file object named file1 . This object can be
used to work with files and directories.
Skip Ad
By default, the files are open in read mode (cannot be modified). The
code above is equivalent to
Mode Description
Open a file for writing. Creates a new file if it does not exist
w
or truncates the file if it exists.
Python Tutorials
Python open()
(/python-
programming/methods/built-
in/open) Here's few simple examples of how to open a file in different modes,
Working with CSV files in Python
(/python-programming/working- file1 = open("test.txt") # equivalent to 'r' or 'rt'
with-csv-files) file1 = open("test.txt",'w') # write in text mode
file1 = open("img.bmp",'r+b') # read and write in binary mode
Python print()
(/python-
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
programming/methods/built-
in/print)
36% off Learn to code by doing. Try hands-on Python with Programiz PRO. Claim Discount Now Reading Files in Python (https://programiz.pro/learn/master-python?utm_source=sticky-
Python JSON
banner&utm_campaign=programiz&utm_medium=referral)
After we open a file, we
Programiz (https://programiz.pro/learn/master-python?utm_source=nav-
(/python-programming/json)
Try use the read() method to read its contents.
Courses Tutorials Examples Search tutorials & examples
(/) PRO floating&utm_campaign=programiz&utm_medium=referral)
Python CSV For example,
www.domain-name.com
(/python-programming/csv)
Output
In the above example, we have read the test.txt file that is available
in our current directory. Notice the code,
read_content = file1.read
Closing a file will free up the resources that were tied with the file. It is
done using the close() method in Python. For example,
# open a file
file1 = open("test.txt", "r")
Output
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
This is a test file.
Hello from the test file.
(https://programiz.pro/learn/master-python?utm_source=sticky-
36% off Learn to code by doing. Try hands-on Python with Programiz PRO. Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-python?utm_source=nav-
Here, we have used the close() method to close the file.
PRO
Courses Tutorials Examples Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
Afterwww.domain-name.com
we perform file operation, we should always close the file; it's a
good programming practice.
Skip Ad
try:
file1 = open("test.txt", "r")
read_content = file1.read()
print(read_content)
finally:
# close the file
file1.close()
Here, we have closed the file in the finally block as finally always
executes, and the file will be closed even if an exception occurs.
Note: Since we don't have to worry about closing the file, make
a habit of using the with...open syntax.
Thank you for printing our contentWriting to Files in Please
at www.domain-name.com. Python
check back soon for new contents.
(https://programiz.pro/learn/master-python?utm_source=sticky-
There are two things we need to remember while writing to a file.
36% off Learn to code by doing. Try hands-on Python with Programiz PRO. Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-python?utm_source=nav-
If we try to open a filetutorials
that doesn't exist, a new file is created.
Courses Tutorials Examples Search & examples
(/) PRO floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
If a file already exists, its content is erased, and new content is
added to the file.
Skip Ad
Here, a new test2.txt file is created and this file will have contents
specified inside the write() method.
Method Description
36% off Learn to code by doing. Try hands-on Python with Programiz PRO. Claim Discount Now Video: Reading and Writing Files in Python
(https://programiz.pro/learn/master-python?utm_source=sticky-
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-python?utm_source=nav-
Courses Tutorials Examples Search tutorials & examples
(/) PRO floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Skip Ad
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
(https://programiz.pro/learn/master-python?utm_source=sticky-
36% off Learn to code by doing. Try hands-on Python with Programiz PRO. Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-python?utm_source=nav-
PRO
Courses Tutorials Examples Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Skip Ad
Previous Next
Tutorial: Tutorial:
(/python- (/python-
Python programming/dictionary) Python programming/directory)
Dictionary Directory
Share on:
(https://www.facebook.com/sharer/sharer.php? (https://twitter.com/intent/tweet?
u=https://www.programiz.com/python- text=Check%20this%20amazing%20article%20on%20Python%20File%20I/O
programming/file-operation) programming/file-operation)
Related Tutorials
Skip Ad