Reading and Writing Files

Slides:



Advertisements
Similar presentations
Unit 6 File processing Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
Advertisements

Slide 1 Introduction To Files In Python In this section of notes you will learn how to read from and write to text files.
Week 7 Lists Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed.
Week 6 review; file processing Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
Week 6 review; file processing Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
INTRODUCTION TO PYTHON PART 4 – TEXT AND FILE PROCESSING CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 13 Files and Exception Handling 1.
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.
Unit 6 File processing Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
ICS 313: Programming Language Theory Chapter 14: Exceptions.
Python – reading and writing files. ??? ???two ways to open a file open and file ??? How to write to relative and absolute paths?
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
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,
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
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])
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
Lecture 4 Python Basics Part 3.
Python Exceptions and bug handling Peter Wad Sackett.
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Python File Handling. Python language provides numerous built-in functions Some of the functions widely used for standard input and output operations.
CMSC201 Computer Science I for Majors Lecture 11 – File I/O (Continued) Prof. Katherine Gibson Based on concepts from:
Prof. Katherine Gibson Prof. Jeremy Dixon
Introduction To Files In Python
CMSC201 Computer Science I for Majors Lecture 10 – File I/O
Topic: File Input/Output (I/O)
CSc 110, Autumn 2017 Lecture 19: While loops and File Input
Example: Vehicles What attributes do objects of Sedan have?
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.
CSc 110, Spring 2017 Lecture 18: Line-Based File Input
Lecture 4 Python Basics Part 3.
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Python’s input and output
Last Class We Covered File I/O How to open a file
Exceptions and files Taken from notes by Dr. Neil Moore
review; file processing
Week 1 Review Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is.
while loops; file I/O; introduction to lists
CSc 110, Autumn 2016 Lecture 16: File Input.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Python’s Errors and Exceptions
Introduction To Files In Python
Adapted from slides by Marty Stepp and Stuart Reges
Exceptions and files Taken from notes by Dr. Neil Moore
Lecture 4 Python Basics Part 3.
Week 7 Lists Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise noted, this.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Adapted from slides by Marty Stepp and Stuart Reges
Introduction To Files In Python
Introduction to Python: Day Three
Programming Training Main Points: - Working with strings.
CSc 110, Spring 2018 Lecture 22: Line-Based File Input
Last Class We Covered Escape sequences File I/O Uses a backslash (\)
CMSC201 Computer Science I for Majors Lecture 13 – File I/O
CSc 110, Spring 2018 Lecture 27: Lists that change size and File Output Adapted from slides by Marty Stepp and Stuart Reges.
CSc 110, Autumn 2016 Programming feel like that?
Last Class We Covered File I/O How to open a file
15-110: Principles of Computing
Topics Introduction to File Input and Output
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Adapted from slides by Marty Stepp and Stuart Reges
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
CSE 231 Lab 5.
Topics Introduction to File Input and Output
Python Exceptions and bug handling
Introduction to Computer Science
Getting Started in Python
Presentation transcript:

Reading and Writing Files The function f=open(filename, mode) opens a file for reading or writing. mode can be: “r” – Reading “w”- Writing “a”- Append “r+”- Open for both reading and writing. “b” – Open the file in binary mode (only in Windows)

File Input/Output input_file = open(“in.txt") output_file = open(“out.txt", "w") for line in input_file: output_file.write(line) “w” = “write mode” “a” = “append mode” “wb” = “write in binary” “r” = “read mode” (default) “rb” = “read in binary” “U” = “read files with Unix or Windows line endings”

Reading a file s = f.read(size) s = f.read() s = f.readline() It will read size characters from the file and place it in string s. s = f.read() It reads the entire file and places it in string s. s = f.readline() It reads a single line from the file. l = f.readlines() It returns a list with all the lines in the file.

Writing to a file f.write(s) f.seek(pos) f.close() It write string s to file f. f.seek(pos) Changes the fileposition to pos f.close() It closes the file

Reading Files name = open("filename") opens the given file for reading, and returns a file object name.read() - file's entire contents as a string name.readline() - next line from file as a string name.readlines() - file's contents as a list of lines the lines from a file object can also be read using a for loop >>> f = open("hours.txt") >>> f.read() '123 Susan 12.5 8.1 7.6 3.2\n 456 Brad 4.0 11.6 6.5 2.7 12\n 789 Jenn 8.0 8.0 8.0 8.0 7.5\n'

File Input Template A template for reading files in Python: name = open("filename") for line in name: statements >>> input = open("hours.txt") >>> for line in input: ... print(line.strip()) # strip() removes \n 123 Susan 12.5 8.1 7.6 3.2 456 Brad 4.0 11.6 6.5 2.7 12 789 Jenn 8.0 8.0 8.0 8.0 7.5

Exercise Write a function input_stats that accepts a file name as a parameter and that reports the longest line in the file. example input file, carroll.txt: Beware the Jabberwock, my son, the jaws that bite, the claws that catch, Beware the JubJub bird and shun the frumious bandersnatch. expected output: >>> input_stats("carroll.txt") longest line = 42 characters the jaws that bite, the claws that catch,

Exercise Solution def input_stats(filename): input = open(filename) longest = "" for line in input: if len(line) > len(longest): longest = line print("Longest line =", len(longest)) print(longest)

String Splitting split breaks a string into tokens that you can loop over. name.split() # break by whitespace name.split(delimiter) # break by delimiter join performs the opposite of a split delimiter.join(list of tokens) >>> name = "Brave Sir Robin" >>> for word in name.split(): ... print(word) Brave Sir Robin >>> "LL".join(name.split("r")) 'BLLave SiLL Robin

Splitting into Variables If you know the number of tokens, you can split them directly into a sequence of variables. var1, var2, ..., varN = string.split() may want to convert type of some tokens: type(value) >>> s = "Jessica 31 647.28" >>> name, age, money = s.split() >>> name 'Jessica' >>> int(age) 31 >>> float(money) 647.28

Tokenizing File Input Use split to tokenize line contents when reading files. You may want to type-cast tokens: type(value) >>> f = open("example.txt") >>> line = f.readline() >>> line 'hello world 42 3.14\n' >>> tokens = line.split() >>> tokens ['hello', 'world', '42', '3.14'] >>> word = tokens[0] 'hello' >>> answer = int(tokens[2]) 42 >>> pi = float(tokens[3]) 3.14

Exercise Suppose we have this hours.txt data: 123 Suzy 9.5 8.1 7.6 3.1 3.2 456 Brad 7.0 9.6 6.5 4.9 8.8 789 Jenn 8.0 8.0 8.0 8.0 7.5 Compute each worker's total hours and hours/day. Assume each worker works exactly five days. Suzy ID 123 worked 31.4 hours: 6.3 / day Brad ID 456 worked 36.8 hours: 7.36 / day Jenn ID 789 worked 39.5 hours: 7.9 / day

Exercise Answer

Writing Files name = open("filename", "w") name = open("filename", "a") opens file for write (deletes previous contents), or opens file for append (new data goes after previous data) name.write(str) - writes the given string to the file name.close() - saves file once writing is done >>> out = open("output.txt", "w") >>> out.write("Hello, world!\n") >>> out.write("How are you?") >>> out.close() >>> open("output.txt").read() 'Hello, world!\nHow are you?'

Exceptions In python the best way to handle errors is with exceptions. Exceptions separates the main code from the error handling making the program easier to read. try: statements except: error handling

Exceptions Example: import sys try: f = open('myfile.txt') s = f.readline() i = int(s.strip()) except IOError as (errno, strerror): print "I/O error({0}): {1}".format(errno, strerror) except ValueError: print "Could not convert data to an integer." except: print "Unexpected error:", sys.exc_info()[0] raise

The finally statement The finally: statement is executed under all circumstances even if an exception is thrown. Cleanup statements such as closing files can be added to a finally stetement.

Finally statement import sys try: f = open('myfile.txt') s = f.readline() i = int(s.strip()) except IOError as (errno, strerror): print "I/O error({0}): {1}".format(errno, strerror) except ValueError: print "Could not convert data to an integer." except: print "Unexpected error:", sys.exc_info()[0] finally: f.close() # Called with or without exceptions.

Error Handling With Exceptions Exceptions are used to deal with extraordinary errors (‘exceptional ones’). Typically these are fatal runtime errors (“crashes” program) Example: trying to open a non-existent file Basic structure of handling exceptions try: Attempt something where exception error may happen except <exception type>: React to the error else: # Not always needed What to do if no error is encountered finally: # Not always needed Actions that must always be performed

Exceptions: File Example Name of the online example: file_exception.py Input file name: Most of the previous input files can be used e.g. “input1.txt” inputFileOK = False while (inputFileOK == False): try: inputFileName = input("Enter name of input file: ") inputFile = open(inputFileName, "r") except IOError: print("File", inputFileName, "could not be opened") else: print("Opening file", inputFileName, " for reading.") inputFileOK = True for line in inputFile: sys.stdout.write(line) print ("Completed reading of file", inputFileName) inputFile.close() print ("Closed file", inputFileName)

Exceptions: File Example (2) # Still inside the body of the while loop (continued) finally: if (inputFileOK == True): print ("Successfully read information from file", inputFileName) else: print ("Unsuccessfully attempted to read information from file", inputFileName)

Exception Handling: Keyboard Input Name of the online example: exception_validation.py inputOK = False while (inputOK == False): try: num = input("Enter a number: ") num = float(num) except ValueError: # Can’t convert to a number print("Non-numeric type entered '%s'" %num) else: # All characters are part of a number inputOK = True num = num * 2 print(num)

Example of using Files >>> f=open("f.py","r") >>> s=f.read() >>> print(s) def fac(n): r=1 for i in range(1,n+1): r = r * i return r import sys n = int(sys.argv[1]) print("The factorial of ", n, " is ", fac(n)) >>> f.close() >>>

Predefined cleanup actions The with statement will automatically close the file once it is no longer in use. with open("myfile.txt") as f: for line in f: print line After finishing the with statement will close the file.

Useful links https://docs.python.org/2/library/stdtypes.html#file-objects https://docs.python.org/2/tutorial/inputoutput.html#reading-and- writing-files http://cmdlinetips.com/2011/08/three-ways-to-read-a-text-file-line- by-line-in-python/ https://www.slideshare.net/Felix11H/python-file-operations-data- parsing-40992696