Python Certification Training https://www.edureka.co/python
Agenda
File Handling In Python
Python Certification Training https://www.edureka.co/python
Agenda
File Handling In Python
Python Certification Training https://www.edureka.co/python
Agenda
Introduction 01
Why need File Handling?
Getting Started 02
Concepts 03
Practical Approach 04
Types of Files
Looking at code to
understand theory
Python File Handling System
Python Certification Training https://www.edureka.co/python
Why need File Handling?
File Handling In Python
Python Certification Training https://www.edureka.co/python
Why Need File Handling?
Python Input
Arguments Standard Input
Files
Python Certification Training https://www.edureka.co/python
Types of Files
File Handling In Python
Python Certification Training https://www.edureka.co/python
Types Of Files
What you may know as a file is slightly different in Python
Text
BinaryImage Text
AudioExecutable
Python Certification Training https://www.edureka.co/python
What is File Handling?
File Handling In Python
Python Certification Training https://www.edureka.co/python
What Is File Handling?
File handling is an important part of any web application
Operations
Creating DeletionReading Updating
CRUD
Python Certification Training https://www.edureka.co/python
Python File Handling System
File Handling In Python
Python Certification Training https://www.edureka.co/python
Python File Handling System
The key function for working with files in Python is the open() function
open()
Filename Mode
Syntax open( filename, mode)
WORK
Create File
Open File
WORK
Close File
Python Certification Training https://www.edureka.co/python
Python File Handling System
The key function for working with files in Python is the open() function
Syntax open( filename, mode)
Any name that you want
Different modes for opening a file
"r" - Read - Default value. Opens a file for reading, error if the file does not exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists
"t" - Text - Default value. Text mode
"b" - Binary - Binary mode (e.g. images)
In addition you can specify if the file should be handled as binary or text mode
Python Certification Training https://www.edureka.co/python
Python File Handling System
Example Code
Example f = open(“demofile.txt”)
Example f = open(“demofile.txt”, “r”)
Note: Make sure file exists or else error!
Python Certification Training https://www.edureka.co/python
File Operations for Reading
File Handling In Python
Python Certification Training https://www.edureka.co/python
Reading Text File In Python
file.read()
Example
> file = open(“testfile.text”, “r”)
> print file.read()
Lots of ways to read a text file in Python
All characters Some characters
Python Certification Training https://www.edureka.co/python
Reading Text File In Python
file.read()
Example
> file = open(“testfile.text”, “r”)
> print file.read()
Example
> file = open(“testfile.text”, “r”)
> print file.read(5) This 5 indicates what?
Python Certification Training https://www.edureka.co/python
Reading Text File In Python
file.read()
Example
> file = open(“testfile.text”, “r”)
> print file.readline(): Line by line output
Example
> file = open(“testfile.text”, “r”)
> print file.readline(3): Read third line only
Example
> file = open(“testfile.text”, “r”)
> print file.readlines(): Read lines separately
Python Certification Training https://www.edureka.co/python
Looping Over A File Object
Fast and efficient!
Example
> file = open(“testfile.text”, “r”)
> for line in file:
> print file.readline():
Looping over the object
Reading from files
Python Certification Training https://www.edureka.co/python
Python File Write Method
File Handling In Python
Python Certification Training https://www.edureka.co/python
File Write Method
Writing to an existing file
To write to an existing file, you must add a parameter to the open()
function:
"a" - Append - will append to the end of the file
"w" - Write - will overwrite any existing content
Example
> f = open("demofile.txt", "a")
> f.write(“ We love Edureka!")
Example
> f = open("demofile.txt", “w")
> f.write(“ We love Edureka!")
Note: the "w" method will
overwrite the entire file.
Python Certification Training https://www.edureka.co/python
File Write Method
Example
> file = open(“testfile.txt”, “w”)
> file.write(“This is a test”)
> file.write(“To add more lines.”)
> file.close()
I’m writing files!
Python Certification Training https://www.edureka.co/python
Creating a New File
File Handling In Python
Python Certification Training https://www.edureka.co/python
Creating A New File
open() method again
➢ file = open(“testfile.txt”, “x”)
➢ file = open(“testfile.txt”, “w”)
To create a new file in Python, use the open() method, with one of the following parameters:
"x" - Create - will create a file, returns an error if the file exist
"a" - Append - will create a file if the specified file does not exist
"w" - Write - will create a file if the specified file does not exist
Python Certification Training https://www.edureka.co/python
Deletion Operations
File Handling In Python
Python Certification Training https://www.edureka.co/python
Deleting A File
os.remove() function
To delete a file, you must import the OS module, and run its os.remove() function:
Example
> import os
> os.remove("demofile.txt")
Deleting a folder?
Example
> import os
> os.rmdir("myfolder")
Check if file exists
> import os
> if os.path.exists("demofile.txt"):
> os.remove("demofile.txt")
> else:
> print("The file does not exist")
Python Certification Training https://www.edureka.co/python
Conclusion
File Handling In Python
Copyright © 2019, edureka and/or its affiliates. All rights reserved.
Python File Handling | File Operations in Python | Learn python programming | Edureka

Python File Handling | File Operations in Python | Learn python programming | Edureka

  • 1.
    Python Certification Traininghttps://www.edureka.co/python Agenda File Handling In Python
  • 2.
    Python Certification Traininghttps://www.edureka.co/python Agenda File Handling In Python
  • 3.
    Python Certification Traininghttps://www.edureka.co/python Agenda Introduction 01 Why need File Handling? Getting Started 02 Concepts 03 Practical Approach 04 Types of Files Looking at code to understand theory Python File Handling System
  • 4.
    Python Certification Traininghttps://www.edureka.co/python Why need File Handling? File Handling In Python
  • 5.
    Python Certification Traininghttps://www.edureka.co/python Why Need File Handling? Python Input Arguments Standard Input Files
  • 6.
    Python Certification Traininghttps://www.edureka.co/python Types of Files File Handling In Python
  • 7.
    Python Certification Traininghttps://www.edureka.co/python Types Of Files What you may know as a file is slightly different in Python Text BinaryImage Text AudioExecutable
  • 8.
    Python Certification Traininghttps://www.edureka.co/python What is File Handling? File Handling In Python
  • 9.
    Python Certification Traininghttps://www.edureka.co/python What Is File Handling? File handling is an important part of any web application Operations Creating DeletionReading Updating CRUD
  • 10.
    Python Certification Traininghttps://www.edureka.co/python Python File Handling System File Handling In Python
  • 11.
    Python Certification Traininghttps://www.edureka.co/python Python File Handling System The key function for working with files in Python is the open() function open() Filename Mode Syntax open( filename, mode) WORK Create File Open File WORK Close File
  • 12.
    Python Certification Traininghttps://www.edureka.co/python Python File Handling System The key function for working with files in Python is the open() function Syntax open( filename, mode) Any name that you want Different modes for opening a file "r" - Read - Default value. Opens a file for reading, error if the file does not exist "a" - Append - Opens a file for appending, creates the file if it does not exist "w" - Write - Opens a file for writing, creates the file if it does not exist "x" - Create - Creates the specified file, returns an error if the file exists "t" - Text - Default value. Text mode "b" - Binary - Binary mode (e.g. images) In addition you can specify if the file should be handled as binary or text mode
  • 13.
    Python Certification Traininghttps://www.edureka.co/python Python File Handling System Example Code Example f = open(“demofile.txt”) Example f = open(“demofile.txt”, “r”) Note: Make sure file exists or else error!
  • 14.
    Python Certification Traininghttps://www.edureka.co/python File Operations for Reading File Handling In Python
  • 15.
    Python Certification Traininghttps://www.edureka.co/python Reading Text File In Python file.read() Example > file = open(“testfile.text”, “r”) > print file.read() Lots of ways to read a text file in Python All characters Some characters
  • 16.
    Python Certification Traininghttps://www.edureka.co/python Reading Text File In Python file.read() Example > file = open(“testfile.text”, “r”) > print file.read() Example > file = open(“testfile.text”, “r”) > print file.read(5) This 5 indicates what?
  • 17.
    Python Certification Traininghttps://www.edureka.co/python Reading Text File In Python file.read() Example > file = open(“testfile.text”, “r”) > print file.readline(): Line by line output Example > file = open(“testfile.text”, “r”) > print file.readline(3): Read third line only Example > file = open(“testfile.text”, “r”) > print file.readlines(): Read lines separately
  • 18.
    Python Certification Traininghttps://www.edureka.co/python Looping Over A File Object Fast and efficient! Example > file = open(“testfile.text”, “r”) > for line in file: > print file.readline(): Looping over the object Reading from files
  • 19.
    Python Certification Traininghttps://www.edureka.co/python Python File Write Method File Handling In Python
  • 20.
    Python Certification Traininghttps://www.edureka.co/python File Write Method Writing to an existing file To write to an existing file, you must add a parameter to the open() function: "a" - Append - will append to the end of the file "w" - Write - will overwrite any existing content Example > f = open("demofile.txt", "a") > f.write(“ We love Edureka!") Example > f = open("demofile.txt", “w") > f.write(“ We love Edureka!") Note: the "w" method will overwrite the entire file.
  • 21.
    Python Certification Traininghttps://www.edureka.co/python File Write Method Example > file = open(“testfile.txt”, “w”) > file.write(“This is a test”) > file.write(“To add more lines.”) > file.close() I’m writing files!
  • 22.
    Python Certification Traininghttps://www.edureka.co/python Creating a New File File Handling In Python
  • 23.
    Python Certification Traininghttps://www.edureka.co/python Creating A New File open() method again ➢ file = open(“testfile.txt”, “x”) ➢ file = open(“testfile.txt”, “w”) To create a new file in Python, use the open() method, with one of the following parameters: "x" - Create - will create a file, returns an error if the file exist "a" - Append - will create a file if the specified file does not exist "w" - Write - will create a file if the specified file does not exist
  • 24.
    Python Certification Traininghttps://www.edureka.co/python Deletion Operations File Handling In Python
  • 25.
    Python Certification Traininghttps://www.edureka.co/python Deleting A File os.remove() function To delete a file, you must import the OS module, and run its os.remove() function: Example > import os > os.remove("demofile.txt") Deleting a folder? Example > import os > os.rmdir("myfolder") Check if file exists > import os > if os.path.exists("demofile.txt"): > os.remove("demofile.txt") > else: > print("The file does not exist")
  • 26.
    Python Certification Traininghttps://www.edureka.co/python Conclusion File Handling In Python
  • 27.
    Copyright © 2019,edureka and/or its affiliates. All rights reserved.