101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    Python

    Receive aemail containing the next unit.
    • Refreshing Python Basics
      • 1.1Python Data Structures
      • 1.2Syntax and Semantics
      • 1.3Conditionals and Loops
    • Introduction to Object-Oriented Programming
      • 2.1Understanding Class and Objects
      • 2.2Design Patterns
      • 2.3Inheritance, Encapsulation, and Polymorphism
    • Python Libraries
      • 3.1Numpy and Matplotlib
      • 3.2Pandas and Seaborn
      • 3.3SciPy
    • Handling Files and Exception
      • 4.1Reading, writing and manipulating files
      • 4.2Introduction to Exceptions
      • 4.3Handling and raising Exceptions
    • Regular Expressions
      • 5.1Introduction to Regular Expressions
      • 5.2Python’s re module
      • 5.3Pattern Matching, Substitution, and Parsing
    • Databases and SQL
      • 6.1Introduction to Databases
      • 6.2Python and SQLite
      • 6.3Presentation of Data
    • Web Scraping with Python
      • 7.1Basics of HTML
      • 7.2Introduction to Beautiful Soup
      • 7.3Web Scraping Case Study
    • Python for Data Analysis
      • 8.1Data cleaning, Transformation, and Analysis using Pandas
      • 8.2Data visualization using Matplotlib and Seaborn
      • 8.3Real-world Data Analysis scenarios
    • Python for Machine Learning
      • 9.1Introduction to Machine Learning with Python
      • 9.2Scikit-learn basics
      • 9.3Supervised and Unsupervised Learning
    • Python for Deep Learning
      • 10.1Introduction to Neural Networks and TensorFlow
      • 10.2Deep Learning with Python
      • 10.3Real-world Deep Learning Applications
    • Advanced Python Concepts
      • 11.1Generators and Iterators
      • 11.2Decorators and Closures
      • 11.3Multithreading and Multiprocessing
    • Advanced Python Concepts
      • 12.1Generators and Iterators
      • 12.2Decorators and Closures
      • 12.3Multithreading and Multiprocessing
    • Python Project
      • 13.1Project Kick-off
      • 13.2Mentor Session
      • 13.3Project Presentation

    Handling Files and Exception

    Reading, Writing, and Manipulating Files in Python

    general-purpose programming language

    General-purpose programming language.

    Python provides several built-in functions and methods for reading, writing, and manipulating files. This article will cover these functions and methods in detail.

    Understanding File Objects

    In Python, a file is treated as an object. The built-in open() function is used to create a file object which provides a connection to the file that resides on your machine.

    file = open('example.txt', 'r')

    In the above example, 'example.txt' is the name of the file and 'r' is the mode which stands for read. There are several modes you can specify when opening a file:

    • 'r': read mode
    • 'w': write mode
    • 'a': append mode
    • 'b': binary mode
    • '+': read and write mode

    Opening and Closing Files

    Once a file is opened and you have done your operations on the file, it is always a good practice to close the file. This is done using the close() method.

    file.close()

    Reading from Files

    Python provides several methods to read from a file:

    • read(): This method reads the entire content of the file.
    • readline(): This method reads the next line of the file.
    • readlines(): This method reads all the lines of the file and returns them as a list.

    Writing to Files

    To write to a file, you need to open it in write 'w', append 'a', or exclusive creation 'x' mode.

    • write(): This method writes a string to the file.
    • writelines(): This method writes a list of strings to the file.

    File Positions

    • tell(): This method returns the current position of the file read/write pointer within the file.
    • seek(offset[, from]): This method changes the current file position. The offset argument indicates the number of bytes to be moved. The from argument specifies the reference position from where the bytes are to be moved.

    Working with CSV and JSON Files

    Python provides the csv and json modules to read and write to CSV and JSON files.

    File Manipulation: Renaming and Deleting Files

    The os module in Python provides functions for creating, renaming, and deleting files.

    • rename(): This method takes two arguments, the current filename and the new filename.
    • remove(): This method deletes the file.

    In conclusion, Python provides a rich set of tools to work with files. It is important to understand these tools to effectively work with data stored in files.

    Test me
    Practical exercise
    Further reading

    Howdy, any questions I can help with?

    Sign in to chat
    Next up: Introduction to Exceptions