1 / 29

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

** Python Certification Training: https://www.edureka.co/python ** <br>This Edureka PPT on File Handling with Python covers all the important aspects of using files in Python right from the introduction to what fields are, all the way till checking out the major aspects of working with files and using the code-first approach to understand them better. <br><br>Python Tutorial Playlist: https://goo.gl/WsBpKe <br>Blog Series: http://bit.ly/2sqmP4s <br><br>Follow us to never miss an update in the future. <br><br>Instagram: https://www.instagram.com/edureka_learning/ <br>Facebook: https://www.facebook.com/edurekaIN/ <br>Twitter: https://twitter.com/edurekain <br>LinkedIn: https://www.linkedin.com/company/edureka

EdurekaIN
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File Handling In Python Agenda Python Certification Training https://www.edureka.co/python

  2. File Handling In Python Agenda Python Certification Training https://www.edureka.co/python

  3. Agenda 01 Introduction Why need File Handling? 02 Getting Started Types of Files 03 Concepts Python File Handling System 04 Practical Approach Looking at code to understand theory Python Certification Training https://www.edureka.co/python

  4. File Handling In Python Why need File Handling? Python Certification Training https://www.edureka.co/python

  5. Why Need File Handling? Python Input Arguments Standard Input Files Python Certification Training https://www.edureka.co/python

  6. File Handling In Python Types of Files Python Certification Training https://www.edureka.co/python

  7. Types Of Files What you may know as a file is slightly different in Python Image Text Binary Executable Audio Text Python Certification Training https://www.edureka.co/python

  8. File Handling In Python What is File Handling? Python Certification Training https://www.edureka.co/python

  9. What Is File Handling? File handling is an important part of any web application CRUD Operations Creating Reading Updating Deletion Python Certification Training https://www.edureka.co/python

  10. File Handling In Python Python File Handling System Python Certification Training https://www.edureka.co/python

  11. Python File Handling System The key function for working with files in Python is the open() function Create File open() Open File Filename Mode WORK WORK Close File Syntax open( filename, mode) Python Certification Training https://www.edureka.co/python

  12. Python File Handling System The key function for working with files in Python is the open() function "r" -Read-Defaultvalue. 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 Syntax open( filename, mode) "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 Any name that you want Different modes for opening a file In addition you can specify if the file should be handled as binary or text mode "t" -Text -Default value. Text mode "b" -Binary -Binary mode (e.g. images) Python Certification Training https://www.edureka.co/python

  13. 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

  14. File Handling In Python File Operations for Reading Python Certification Training https://www.edureka.co/python

  15. Reading Text File In Python file.read() Lots of ways to read a text file in Python All characters Some characters > file = open(“testfile.text”, “r”) > print file.read() Example Python Certification Training https://www.edureka.co/python

  16. Reading Text File In Python file.read() > file = open(“testfile.text”, “r”) > print file.read(5) Example This 5 indicates what? > file = open(“testfile.text”, “r”) > print file.read() Example Python Certification Training https://www.edureka.co/python

  17. Reading Text File In Python file.read() > file = open(“testfile.text”, “r”) > print file.readline(): Example Line by line output > file = open(“testfile.text”, “r”) > print file.readline(3): Example Read third line only > file = open(“testfile.text”, “r”) > print file.readlines(): Example Read lines separately Python Certification Training https://www.edureka.co/python

  18. Looping Over A File Object Fast and efficient! > file = open(“testfile.text”, “r”) > for line in file: > print file.readline(): Example Looping over the object Reading from files Python Certification Training https://www.edureka.co/python

  19. File Handling In Python Python File Write Method Python Certification Training https://www.edureka.co/python

  20. 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 > f = open("demofile.txt", "a") > f.write(“ We love Edureka!") Example Note: the "w" method will overwrite the entire file. > f = open("demofile.txt", “w") > f.write(“ We love Edureka!") Example Python Certification Training https://www.edureka.co/python

  21. 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

  22. File Handling In Python Creating a New File Python Certification Training https://www.edureka.co/python

  23. Creating A New File open() method again 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 ➢ file = open(“testfile.txt”, “x”) ➢ file = open(“testfile.txt”, “w”) Python Certification Training https://www.edureka.co/python

  24. File Handling In Python Deletion Operations Python Certification Training https://www.edureka.co/python

  25. Deleting A File os.remove() function To delete a file, you must import the OS module, and run its os.remove() function: > import os > os.remove("demofile.txt") Example Check if file exists Deleting a folder? > import os > if os.path.exists("demofile.txt"): > os.remove("demofile.txt") > else: > print("The file does not exist") > import os > os.rmdir("myfolder") Example Python Certification Training https://www.edureka.co/python

  26. File Handling In Python Conclusion Python Certification Training https://www.edureka.co/python

  27. Copyright © 2019, edureka and/or its affiliates. All rights reserved.

More Related