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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Python’s input and output Chenghao Wang. Fancier Output Formatting – Output method ▪Print() ▪Str() ▪Repr() Example S=“Hello World” Print(s) OR Print(“Hello.
Computer Science 111 Fundamentals of Programming I Files.
CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
Lecture 03 – Sequences of data.  At the end of this lecture, students should be able to:  Define and use functions  Import functions from modules 
Working with Files CSC 161: The Art of Programming Prof. Henry Kautz 11/9/2009.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 17 Reading and Writing Files 5/10/09 Python Mini-Course: Lesson 17 1.
CompSci 101 Introduction to Computer Science January 20, 2015 Prof. Rodger compsci 101, spring
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
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.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s)
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8.
1 Chapter 7 – Object-Oriented Programming and File Handling spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
Computer Programming TCP1224 Chapter 13 Sequential File Access.
Python: Input and Output Yuen Kwan Lo. Output Format str( ) and repr( ) same representation but String and Floating point number a=0.24 str(a)‘0.24’ repr.
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,
 Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python.
 Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper.
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])
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
Input and Output in python Josh DiCristo. How to output numbers str()  You can put any variable holding a number inside the parentheses and it will display.
Lecture 4 Python Basics Part 3.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
Files A collection of related data treated as a unit. Two types Text
1 Essential Computing for Bioinformatics Bienvenido Vélez UPR Mayaguez Lecture 3 High-level Programming with Python Part III: Files and Directories Reference:
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Programming Training Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Kanel Nang.  Two methods of formatting output ◦ Standard string slicing and concatenation operations ◦ str.format() method ie. >>> a = “The sum of 1.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
11/13/07. >>> Overview * lists/arrays * reading files * writing files.
Programming with ANSI C ++
COMPSCI 107 Computer Science Fundamentals
Topic: File Input/Output (I/O)
File I/O File input/output Iterate through a file using for
CompSci 6 Introduction to Computer Science
File Writing Upsorn Praphamontripong CS 1110
TMF1414 Introduction to Programming
Python’s input and output
Programming in C Input / Output.
Class 9 Reading and writing to files chr, ord and Unicode
Binary Files.
Lecture 13 Input/Output Files.
Topics Introduction to File Input and Output
File IO and Strings CIS 40 – Introduction to Programming in Python
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Fundamentals of Programming I Files
File I/O File input/output Iterate through a file using for
File I/O File input/output Iterate through a file using for
File Handling.
CS 1111 Introduction to Programming Fall 2018
Using Text Files in Python
Programming Training Main Points: - Working with strings.
Input and Output with FILES
Files Handling In today’s lesson we will look at:
Strings, Lists, and Files
15-110: Principles of Computing
Topics Introduction to File Input and Output
CSE 231 Lab 5.
Topics Introduction to File Input and Output
CS 1111 Introduction to Programming Spring 2019
Introduction to Computer Science
Presentation transcript:

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 code:  Word count of a text file  Sum the numbers in a text file 2COMPSCI Computer Science Fundamentals

 Random numbers are pseudo-random  Use a seed value  import random  random.seed(x)  random.choice( ) 3COMPSCI Computer Science Fundamentals

 Get a reference to the file on disk  open(filename, mode)  Modes  ‘r’ = read  ‘w’ = write  ‘r+’ = read and write  Default is text mode, append ‘b’ for binary 4COMPSCI Computer Science Fundamentals f = open(filename) f = open(filename, 'r') f = open(filename, 'wb')

 Different ways to read the contents of the file  When the end of file is reached, empty string is returned 5COMPSCI Computer Science Fundamentals for line in f: print(line) #do something with the line f.readline() # returns a single line (string) from the file f.read() # returns the entire file as a string f.readlines() # returns a list of lines (strings) from the file

 With  automatically closes the file  handles some of the exceptions 6COMPSCI Computer Science Fundamentals with open('filename') as f: data = f.readlines() with open('filename', 'w') as f: f.write('Hello World') with open('filename') as f: for line in f: print(line)

7COMPSCI Computer Science Fundamentals def read_data(filename): '''Reads data from a file Given the name of a file, reads all the data from a file and return the data as a list where each element in the list is a single piece of datum from the file. test2.txt contains the text: This is a small test file with a few words. >>> read_data('test2.txt') ['This', 'is', 'a', 'small', 'test', 'file', 'with', 'a', 'few', 'words.'] ''' data = [] with open(filename) as f: for line in f: data += line.split() return data

 Write a function that returns the word count for a text file.  Write a function that returns the number of unique words in a text file (where words are defined as a sequence of characters separated by white space). 8COMPSCI Computer Science Fundamentals

 Read data as text  Convert text to number  Functions that change types  int()  float()  str() 9COMPSCI Computer Science Fundamentals for line in f: number = int(line) #one value per line

 Write a function that reads in a file of numbers and returns the sum of those numbers. 10COMPSCI Computer Science Fundamentals

 Write a function called unique_words that accepts the name of a text file as an argument and produces a list of all the words in that file, without repeating any of the words. 11COMPSCI Computer Science Fundamentals

 Write a function called difference that accepts two file names as arguments. The function should print out any line in the first file that is not identical to the same line in the second file 12COMPSCI Computer Science Fundamentals

 Similar to reading data  Write all the data  Close the file after all the data is written. 13COMPSCI Computer Science Fundamentals f.write( string_to_write ) f.close()

 Write a function called word_list that accepts the name of a text file as an argument and produces a list of all the words in that file, sorted in order with one word per line. 14COMPSCI Computer Science Fundamentals

 Files need to be opened before you can read or write to them  f = open( filename[, mode] )  Files should be closed when you finish with them.  f.close()  Reading a file  f.read()  f.readline()  Writing a file  f.write( string ) 15COMPSCI Computer Science Fundamentals