Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Functions and Classes: Reading and Writing Files, Exceptions, and Storing Data in Python, Schemes and Mind Maps of Programming Paradigms

Programming LanguagesSoftware EngineeringData Structures and Algorithms

This chapter from the Python programming course covers various aspects of functions and classes, including reading and writing files, handling exceptions, and storing data using JSON. Topics include reading an entire file, reading line by line, making a list of lines, writing to a file, handling ZeroDivisionError exceptions, and using try-except blocks. Students will also learn how to count the frequency of each word in a list of filenames and store data using JSON.

What you will learn

  • How can you count the frequency of each word in a list of filenames in Python?
  • How can you handle ZeroDivisionError exceptions in Python?
  • What is JSON and how is it used to store data in Python?
  • How do you read an entire file in Python?
  • What is the difference between 'w' and 'a' modes when writing to a file in Python?

Typology: Schemes and Mind Maps

2021/2022

Uploaded on 10/28/2022

thanh-duong-nhat
thanh-duong-nhat 🇻🇳

10 documents

1 / 17

Toggle sidebar

Related documents


Partial preview of the text

Download Functions and Classes: Reading and Writing Files, Exceptions, and Storing Data in Python and more Schemes and Mind Maps Programming Paradigms in PDF only on Docsity! Lecturer: Nguyen Tuan Long, Phd Email: ntlong@neu.edu.vn Mobile: 0982 746 235 Chapter 2: Functions and classes • Reading from a File • Writing to a File • Exception • Storing Data Chapter 2: Functions and classes 2 2.3. File and Execptions Reading from a File Reading Line by Line 1 filename = 'pi_digits.txt' 2 with open(filename) as file_object: 3 print(type(file_object) ) <class '_io.TextIOWrapper' > 1 filename = 'pi_digits.txt' 1 filename = ‘pi_digits.txt' 2 with open(filename) as file object: 2 with open(filename) as file_object: 3 for line in file_object: 3 for line in file object: 4 4 print(line) print(line.rstrip()) 3.1415926535 3.1415926535 8979323846 8979323846 2643383279 2643383279 Reading from a File Making a List of Lines from a File Working with a File’s Contents Reading from a File Large Files: One Million Digits Is Your Birthday Contained in Pi? Writing to a File Appending to a File 1 filename = 'programming.txt' 2 3 with open(filename,'a') as file_object: 4 file _object.write("I also love finding meaning in large datasets.\n") 5 file_object.write("I love creating apps that can run in a browser.\n") I love programming. I love creating new games. 3 I also love finding meaning in large datasets. I love creating apps that can run in a browser. ui pb Practice 10-3,…,10-5 Handling the ZeroDivisionError Exception 1 print(5/@) ZeroDivisionError Traceba ck (most recent call last) <ipython-input -28-fad876a50e27> in <module> ----> 1 print(5/@) ZeroDivisionError};, division by zero Using try-except Blocks try: print(5/@) except |ZeroDivisionError print("You can't divide by zero!") BWNPR You can't divide by zero! Counting… Count the frequency of each word in filenames = ['alice.txt', 'siddhartha.txt', 'moby_dick.txt', 'little_women.txt'] Practice 10-6,…, 10-10 Storing Data Using json.dump() and json.load() The JSON (JavaScript Object Notation)
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved