15-110: Principles of Computing

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Computer Science 111 Fundamentals of Programming I Files.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling.
Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Topics This week: File input and output Python Programming, 2/e 1.
Working with Files CSC 161: The Art of Programming Prof. Henry Kautz 11/9/2009.
Lecture 16 – Open, read, write and close files.  At the end of this lecture, students should be able to:  understand file structure  open and close.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
CIT 590 Intro to Programming Files etc. Announcements From HW5 onwards (HW5, HW6,…) You can work alone. You can pick your own partner. You can also stick.
File I/O Ruth Anderson UW CSE 160 Spring File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Files Tutor: You will need ….
PC204 Lecture 5 Conrad Huang Genentech Hall, N453A
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
Lecture 4 Python Basics Part 3.
IIITD File Input / Output In Python. File and operations  File is a named location on disk to store related information  When we want to read from or.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
COMPSCI 107 Computer Science Fundamentals
Topic: File Input/Output (I/O)
Fundamentals of Python: First Programs
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
File I/O File input/output Iterate through a file using for
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Chapter 7 Text Input/Output Objectives
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
Python’s input and output
Ruth Anderson UW CSE 160 Winter 2016
Ruth Anderson UW CSE 160 Winter 2017
And now for something completely different . . .
Ruth Anderson UW CSE 160 Spring 2018
File Handling Programming Guides.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
File IO and Strings CIS 40 – Introduction to Programming in Python
Fundamentals of Programming I Files
File I/O File input/output Iterate through a file using for
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
File I/O File input/output Iterate through a file using for
File Handling.
Introduction to Python: Day Three
Text / Serial / Sequential Files
Rocky K. C. Chang 30 October 2018 (Based on Zelle and Dierbach)
Files Handling In today’s lesson we will look at:
Fundamentals of Python: First Programs
15-110: Principles of Computing
CS190/295 Programming in Python for Life Sciences: Lecture 3
15-110: Principles of Computing
Strings, Lists, and Files
15-110: Principles of Computing
15-110: Principles of Computing
15-110: Principles of Computing
15-110: Principles of Computing
15-110: Principles of Computing
Topics Introduction to File Input and Output
15-110: Principles of Computing
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Topics Introduction to File Input and Output
How to read from a file read, readline, reader
Files Chapter 8.
Presentation transcript:

15-110: Principles of Computing File Processing Lecture 17, November 06, 2018 Mohammad Hammoud Carnegie Mellon University in Qatar

Today… Last Session: Today’s Session: Announcement: Sequences- Part IV (Dictionaries) Today’s Session: Files: Why Files File Processing File Functions Examples Announcement: HA05 will be out on November 08. It is due on November 17 by midnight

Files All data that we store in lists, tuples, and dictionaries within any program are lost when the program ends These data structures are kept in volatile memory (i.e., RAM) To save data for future accesses, we can use files, which are stored on non-volatile memory (usually on disk drives) Files can contain any data type, but the easiest files to work with are those that contain text You can think of a text file as a (possibly long) string that happens to be stored on disk!

File Processing Python has several functions for creating, reading, updating, and deleting files To create and/or open an existing file in Python, you can use the open() function as follows: <variable> = open(<file_name>, <mode>) <variable> is a file object that you can use in your program to process (i.e., read, write, update, or delete) the file

File Processing <file_name> is a string that defines the name of the file on disk <mode> can be only 1 of the following four modes: "r“: This is the default mode; It allows opening a file for reading; An error will be generated if the file does not exist "a“: It allows opening a file for appending; It creates the file if it does not exist "w“: It allows opening a file for writing; It creates the file if it does not exist "x“: It allows creating a specified file; It returns an error if the file exists

File Processing In addition, you can specify if the file should be handled as a binary (e.g., image) or text file "t“: This is the default value; It allows handling the file as a text file "b“: It allows handling the file as a binary fine Example: f = open("file1.txt") is equivalent to f = open("file1.txt“, “rt”) When opening a file for reading, make sure it exists, or otherwise you will get an error!

Reading a File The open() function returns a file object, which has a read() method for reading the content of the file f = open("file1.txt", "r") data = f.read() print(data) print(type(data)) The type of data is str, which you can parse as usual!

Reading a File You can also specify how many characters to return from a file using read(x), where x is the first x characters in the file f = open("file1.txt", "r") data = f.read(10) print(data) print(type(data)) This will return the first 10 characters in the file

Reading a File You can also use the function readline() to read one line at a time f = open("file1.txt", "r") str1 = f.readline() print(str1) str2 = f.readline() print(str2) print(f.readline()) This file contains some information about sales Total sales today = QAR100,000 Sales from PCs = QAR70,000 This is the content of the file

Reading a File You can also use the function readline() to read one line at a time f = open("file1.txt", "r") str1 = f.readline() print(str1) str2 = f.readline() print(str2) print(f.readline()) This file contains some information about sales Total sales today = QAR100,000 Sales from PCs = QAR70,000 Note that the string returned by readline() will always end with a newline, hence, two newlines were produced after each sentence, one by readline() and another by print()

Reading a File You can also use the function readline() to read one line at a time f = open("file1.txt", "r") str1 = f.readline() print(str1, end = “”) str2 = f.readline() print(str2, end = “”) print(f.readline(), end = “”) This file contains some information about sales Total sales today = QAR100,000 Sales from PCs = QAR70,000 Only a solo newline will be generated by readline(), hence, the output on the right side

Looping Through a File Like strings, lists, tuples, and dictionaries, you can also loop through a file line by line f = open("file1.txt", "r") for i in f: print(i, end = "") This file contains some information about sales Total sales today = QAR100,000 Sales from PCs = QAR70,000 Again, each line returned will produce a newline, hence, the usage of the end keyword in the print function

Writing to a File To write to a file, you can use the “w” or the “a” mode in the open() function alongside the write() function as follows: f = open("file1.txt", "w") f.write("This file contains some information about sales\n") f.write("Total sales today = QAR100,000\n") f.write("Sales from PCs = QAR70,000\n") f.close() Every time we run this code, the same above content will be written (NOT appended) to file1.txt since we are using the “w” mode If, alternatively, we use the “a” mode, each time we run the code, the same above content will be appended to the end of file1.txt

Writing to a File To write to a file, you can use the “w” or the “a” mode in the open() function alongside the write() function as follows: f = open("file1.txt", "w") f.write("This file contains some information about sales\n") f.write("Total sales today = QAR100,000\n") f.write("Sales from PCs = QAR70,000\n") f.close() Also, once we are finished writing to the file, we should always close it using the close() function. This will ensure that the written content are pushed from volatile memory (i.e., RAM) to non-volatile memory (i.e., disk), thus preserved

Writing to a File You can also write information into a text file via the already familiar print() function as follows: f = open("file1.txt", "w") print("This file contains some information about sales", file = f) print("Total sales today = QAR100,000", file = f) print("Sales from PCs = QAR70,000", file = f) f.close() This behaves exactly like a normal print, except that the result will be sent to a file rather than to the screen

Deleting a File To delete a file, you must first import the OS module, and then run its os.remove() function You can also check if the file exists before you try to delete it import os os.remove("file1.txt") import os if os.path.exists("file1.txt"): os.remove("file1.txt") else: print("The file does not exist")

Example: Copy a File import os def copyFile(name): if type(name) is str and os.path.exists(name): l = name.split(".") new_name = l[0] + "_copy." + l[1] f_source = open(name, "r") f_destination = open(new_name, "w") f_destination.write(f_source.read()) f_source.close() f_destination.close() else: print("The file does not exist!") copyFile("file1.txt")

Example: Word Count A common utility on Unix/Linux systems is a small program called “wc” which, if called for any file, returns the numbers of lines, words, and characters contained therein We can create our version of “wc” in Python as follows: import os def wordCount(name): if type(name) is str and os.path.exists(name): f = open(name) l_c = 0 w_c = 0

Example: Word Count c_c = 0 for i in f: l_c = l_c + 1 list_of_words = i.split() w_c = w_c + len(list_of_words) for j in list_of_words: c_c = c_c + len(j) dic = {"Line Count": l_c, "Word Count": w_c, "Character Count": c_c} return dic else: print("The file does not exist!") print(wordCount("file1.txt"))

Next Class Practicing & Discussing HW05 Problems …