Files Introduction to Computing Science and Programming I.

Slides:



Advertisements
Similar presentations
Parts of a Computer.
Advertisements

The INFILE Statement Reading files into SAS from an outside source: A Very Useful Tool!
EEE 435 Principles of Operating Systems Principles and Structure of I/O Software (Modern Operating Systems 5.2 & 5.3) 5/22/20151Dr Alain Beaulieu.
Lists Introduction to Computing Science and Programming I.
1 File Output. 2 So far… So far, all of our output has been to System.out  using print(), println(), or printf() All input has been from System.in 
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Introduction to Python
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Files in Python The Basics. Why use Files? Very small amounts of data – just hardcode them into the program A few pieces of data – ask the user to input.
Computer Science A 1: 3/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
CS 0008 Day 2 1. Today Hardware and Software How computers store data How a program works Operators, types, input Print function Running the debugger.
Working with Files CSC 161: The Art of Programming Prof. Henry Kautz 11/9/2009.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Why Program? Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer.
Chapter Introduction to Computers and Programming 1.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
General Programming Introduction to Computing Science and Programming I.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
Chapter 9-Text File I/O. Overview n Text File I/O and Streams n Writing to a file. n Reading from a file. n Parsing and tokenizing. n Random Access n.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
5 1 Data Files CGI/Perl Programming By Diane Zak.
You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Files: By the end of this class you should be able to: Prepare for EXAM 1. create an ASCII file describe the nature of an ASCII text Use and describe string.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Files Tutor: You will need ….
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Python Let’s get started!.
Files in Python The Basics. Why use Files? Very small amounts of data – just hardcode them into the program A few pieces of data – ask the user to input.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Chapter 1: Introduction to Computers and Programming.
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()
BASIC PROGRAMMING C SCP1103 (02)
Introduction to Computing Science and Programming I
Fundamentals of Python: First Programs
Operating System Review
Python: Experiencing IDLE, writing simple programs
Files and Exceptions: The Trivia Challenge Game
Python Let’s get started!.
BASIC PROGRAMMING C SCP1103 (02)
Introduction to Computing Science and Programming I
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Operating System Review
Python I/O.
Python Primer 2: Functions and Control Flow
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
Using files Taken from notes by Dr. Neil Moore
Interaction of Hardware
Fundamentals of Data Structures
15-110: Principles of Computing
Topics Introduction to File Input and Output
Python Basics with Jupyter Notebook
Introduction to Computer Science
Topics Introduction to File Input and Output
Introduction to Computer Science
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Files Introduction to Computing Science and Programming I

Files So far we’ve just sent output to and taken input from the user. So far we’ve just sent output to and taken input from the user. Python makes it relatively easy to write to and read from files. Python makes it relatively easy to write to and read from files. We are only going to deal with simple ASCII text files We are only going to deal with simple ASCII text files

File Output Before you can write to a file you have to open it with a line like this. Before you can write to a file you have to open it with a line like this. fileout = file(“output.txt”,”w”) Notice that you are creating an object of the type file. The “w” means that you want to write to the file. Notice that you are creating an object of the type file. The “w” means that you want to write to the file. After which you will do all of the writing to the file with statements such as this. After which you will do all of the writing to the file with statements such as this. fileout.write(“This will be written in the file\n”) After all of the writing has finished you have to close the file. After all of the writing has finished you have to close the file.fileout.close()

File Output When printing output to the screen, Python automatically adds a newline character, but we need to add these manually when sending output to a file. When printing output to the screen, Python automatically adds a newline character, but we need to add these manually when sending output to a file. fileout.write(“This is the first line.\n”) fileout.write(“This is the second.\n”)

File Output If instead of just of a bunch of regular text, you need to output more organized information such as a class roster, you can print the different pieces of info separated by commas, or some other delimiter. If instead of just of a bunch of regular text, you need to output more organized information such as a class roster, you can print the different pieces of info separated by commas, or some other delimiter.

File Output #assume nameList, idList, and yearList have been filled with data #Each line will contain the name, id, and year of one student fileOut = file(“studentInfo.txt”,”w”) for index in range(len(nameList)): fileOut.write(nameList[index] + “,”) fileOut.write(nameList[index] + “,”) fileOut.write(idList[index] + “,”) fileOut.write(idList[index] + “,”) fileOut.write(str(yearList[index]) + “\n”) fileOut.write(str(yearList[index]) + “\n”)fileOut.close()

File Output Files like the one created by the previous code are known as comma-separated value files or csv files. Files like the one created by the previous code are known as comma-separated value files or csv files. These can be taken as input by most spreadsheet programs. These can be taken as input by most spreadsheet programs. Python provides a csv module that simplifies the use of these csv files. Python provides a csv module that simplifies the use of these csv files.

File Output When you open a file to write to it in this manner, you erase everything that was in the file previously. When you open a file to write to it in this manner, you erase everything that was in the file previously. Unless you close your file, your output will not be saved. Every time you create a file object for input or output, you should call the close() method when you are finished with it. Unless you close your file, your output will not be saved. Every time you create a file object for input or output, you should call the close() method when you are finished with it.

File Input To open a file for input you create a file object in the same way as for output, except you replace the “w” what a “r” to read from the file. To open a file for input you create a file object in the same way as for output, except you replace the “w” what a “r” to read from the file. fileIn = file(“input.txt”,”r”) You can use a for loop to work with the file one line at a time. You can use a for loop to work with the file one line at a time. for line in fileIn: print len(line) print len(line) At the end you should close the file At the end you should close the filefileIn.close()

File Input Remember that every line in the file ends with a newline ‘\n’ character. This character will be included in each line that you read from a file. Remember that every line in the file ends with a newline ‘\n’ character. This character will be included in each line that you read from a file. A couple ways you can deal with this. A couple ways you can deal with this. line = line[:-1] line = line[:-1] line = line.rstrip() line = line.rstrip() This is a method that removes all of the whitespace (spaces, newlines…) from the end of a string. This is a method that removes all of the whitespace (spaces, newlines…) from the end of a string.

File Input Reading in the student info we wrote earlier Reading in the student info we wrote earlier fileIn = file("studentInfo.txt","r") nameList = [] IDList = [] yearList = [] for line in fileIn: line = line.rstrip() line = line.rstrip() name,ID,year = line.split(",") name,ID,year = line.split(",") nameList.append(name) nameList.append(name) IDList.append(ID) IDList.append(ID) yearList.append(int(year)) yearList.append(int(year))fileIn.close()

File Input The split method returns the list of parts of a string separated by the character you give it. The split method returns the list of parts of a string separated by the character you give it. name,ID,year = line.split(",") This is a shorthand you can use to assign the values of a list to multiple variables. The following code is equivalent. This is a shorthand you can use to assign the values of a list to multiple variables. The following code is equivalent. student = line.split(",") student = line.split(",") name = student[0] name = student[0] ID = student[1] ID = student[1] year = student[2] year = student[2]

Operating System When you write your program in Python you don’t have to worry about the specifics of how files are stored on disk. When you write your program in Python you don’t have to worry about the specifics of how files are stored on disk. The operating system is the piece of software that runs on a computer that communicates with the hardware. The operating system is the piece of software that runs on a computer that communicates with the hardware. Applications (including your programs) communicate with the operating system which then works with the hardware. Applications (including your programs) communicate with the operating system which then works with the hardware.

Operating System

Disks All types of disk (hard disk, floppy disk, cd, usb drive…) store information in more or less the same way. All types of disk (hard disk, floppy disk, cd, usb drive…) store information in more or less the same way. The disk is split up into blocks, often 4 kb in size, that are used for organization. The disk is split up into blocks, often 4 kb in size, that are used for organization. When writing a file that is larger than the block size, the file has to split up over multiple blocks which may not be adjacent. When writing a file that is larger than the block size, the file has to split up over multiple blocks which may not be adjacent. Of course this is all handled behind the scenes by the OS. Of course this is all handled behind the scenes by the OS.

Disks

Disks Reading a file spread over blocks like this is inefficient. The file is said to be fragmented. Reading a file spread over blocks like this is inefficient. The file is said to be fragmented. If you defragment your drive, the file will be stored in consecutive blocks, making reading of the file easier. If you defragment your drive, the file will be stored in consecutive blocks, making reading of the file easier.

Disks