Introduction to Computing Science and Programming I

Slides:



Advertisements
Similar presentations
Parts of a Computer.
Advertisements

Files Introduction to Computing Science and Programming I.
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
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.
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.
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.
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 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)
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()
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
BASIC PROGRAMMING C SCP1103 (02)
Development Environment
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)
Variables, Expressions, and IO
Arrays and files BIS1523 – Lecture 15.
Getting Started with C.
Functions CIS 40 – Introduction to Programming in Python
Intro to PHP & Variables
Operating System Review
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
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
Week 9 – Lesson 1 Arrays – Character Strings
Data Structures – 1D Lists
Topics Introduction Hardware and Software How Computers Store Data
Fundamentals of Data Structures
Chapter 2: The Linux System Part 5
Fundamentals of Python: First Programs
Stata Basic Course Lab 2.
CISC101 Reminders All assignments are now posted.
15-110: Principles of Computing
Topics Introduction to File Input and Output
Python Basics with Jupyter Notebook
Introduction to Computer Science
Introduction to Computer Science
Topics Introduction to File Input and Output
Introduction to Computer Science
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Introduction to Computing Science and Programming I Files Introduction to Computing Science and Programming I

Files 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. 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. 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. 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. 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. 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.

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(idList[index] + “,”) 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. These can be taken as input by most spreadsheet programs. 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. 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. fileIn = file(“input.txt”,”r”) You can use a for loop to work with the file one line at a time. for line in fileIn: print len(line) At the end you should close the file fileIn.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. A couple ways you can deal with this. line = line[:-1] line = line.rstrip() 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 fileIn = file("studentInfo.txt","r") nameList = [] IDList = [] yearList = [] for line in fileIn: line = line.rstrip() name,ID,year = line.split(",") nameList.append(name) IDList.append(ID) 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. 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. student = line.split(",") name = student[0] ID = student[1] 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. 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.

Operating System

Disks 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. 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.

Disks

Disks 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.

Disks