SlideShare a Scribd company logo
Computer Science 151
An introduction to the art of computing
File Input and Ouptut
Rudy Martinez
CS151 Spring 2019
Opening Files in Python
● Open() function
○ Returns a file object
○ myFile = open(‘filename’, ‘mode’)
■ Mode:
● ‘r’ - Read only
● ‘w’ - write, allows you to write and edit files
○ This will overwrite an existing file! Be careful.
● ‘a’ - append will add to the end of the file.
myfile=open(‘Moby_Dick_by_Mellville.txt’, ‘r’)
CS151 Spring 2019
Using a file object
myfile=open(‘Moby_Dick_by_Mellville.txt’, ‘r’)
● To read a single line.
○ myfile.readline()
■ This returns a string
● To read all lines into a list.
○ textList = myfile.readlines()
■ This returns a list
CS151 Spring 2019
How to read all lines
● textList = myfile.readlines()
● How do we go through a list?
○ For loop!
# Open file
myfile=open('/home/rudym/Downloads/Moby_Dick_by_Melville.txt', 'r')
# Load each line in file to list textList
textList = myfile.readlines()
# For loop to read and print each line
for i in range(0, len(textList), 1):
print(textList[i])
CS151 Spring 2019
Writing Files
● Writing files is similar to reading files.
● The main misstep is using ‘w’ instead of ‘a’ when you have a file you want to
add to.
○ ‘w’ will write over any file
○ ‘a’ will append to the end of the file preserving previous entries
outfile = open(‘Filename’, ‘w’)
CS151 Spring 2019
Writing files
● Also like read, there are two main functions for putting text into a file.
○ write(‘string’) - will write a passed string into the file and does not append a carriage return
○ writelines( string, list, or dictionary ) - takes anything that can be iterated
■ Iterables are strings, list and dictionaries
outfile = open(‘test.txt’, ‘w’)
for i in range(0, 10, 1):
outfile.write(str(i))
Output: 0123456789
outfile = open(‘test.txt’, ‘w’)
myString = ‘Hello Cruel World’
outfile.writelines(myString)
Output: Hello Cruel World
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[H]
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[He]
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hel]
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hell]
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello]
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello ]
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello C]
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cr]
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cru]
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Crue]
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cruel]
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cruel ]
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cruel W]
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cruel Wo]
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cruel Wor]
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cruel Worl]
CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cruel World]
CS151 Spring 2019
For loops revisited!
Old For loop
for i in range(0, len(myString), 1):
print(myString[i])
Quick Loop
for i in myString:
print(i]
The two for loops below do the exact same thing, but one is short hand!
Why didn’t you teach us this first?!?*?
CS151 Spring 2019
To Sum it up
● Open returns a file object for reading and writing.
● Reading:
○ readline() - Single line of text
○ readlines() - All lines into list
● Writing:
○ writeline() - Single string to file
○ writelines() - An iterable to the file
CS151 Spring 2019
Friday
● 2/15 Homework 2 part A assigned
● 2/20 Homework 2 part A DUE! (5 days)
● 2/22 Homework 2 part B assigned
● 2/27 Homework 2 part B DUE! (5 days)
● 3/1 Homework 2 part C assigned
● 3/4 Exam Review!!!
● 3/6 Midterm Exam
● 3/29 Homework 2 part C DUE! (nearly a month)

More Related Content

What's hot

Data structure and its types
Data structure and its typesData structure and its types
Data structure and its typesNavtar Sidhu Brar
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
Zaid Shabbir
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
NalinNishant3
 
Doubly linklist
Doubly linklistDoubly linklist
Link List
Link ListLink List
Link List
Budditha Hettige
 
Algorithm and Data Structure - Queue
Algorithm and Data Structure - QueueAlgorithm and Data Structure - Queue
Algorithm and Data Structure - Queue
AndiNurkholis1
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structure
faran nawaz
 
basics of queues
basics of queuesbasics of queues
basics of queues
sirmanohar
 
3 searching algorithms in Java
3 searching algorithms in Java3 searching algorithms in Java
3 searching algorithms in Java
Mahmoud Alfarra
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURE
Rohit Rai
 
Algorithm and Data Structure - Stack
Algorithm and Data Structure - StackAlgorithm and Data Structure - Stack
Algorithm and Data Structure - Stack
AndiNurkholis1
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
Vivek Kumar Sinha
 
Stack and queue
Stack and queueStack and queue
Stack and queue
Anil Kumar Prajapati
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its types
Rameesha Sadaqat
 
Random thoughts on... data bloat
Random thoughts on... data bloatRandom thoughts on... data bloat
Random thoughts on... data bloat
Nigel Foulkes-Nock
 
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Hassan Ahmed
 
Data structure power point presentation
Data structure power point presentation Data structure power point presentation
Data structure power point presentation
Anil Kumar Prajapati
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueGhaffar Khan
 
Data structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithmsData structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithms
Abimbola Idowu
 

What's hot (20)

Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Doubly linklist
Doubly linklistDoubly linklist
Doubly linklist
 
Link List
Link ListLink List
Link List
 
Algorithm and Data Structure - Queue
Algorithm and Data Structure - QueueAlgorithm and Data Structure - Queue
Algorithm and Data Structure - Queue
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structure
 
basics of queues
basics of queuesbasics of queues
basics of queues
 
3 searching algorithms in Java
3 searching algorithms in Java3 searching algorithms in Java
3 searching algorithms in Java
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURE
 
Algorithm and Data Structure - Stack
Algorithm and Data Structure - StackAlgorithm and Data Structure - Stack
Algorithm and Data Structure - Stack
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its types
 
Random thoughts on... data bloat
Random thoughts on... data bloatRandom thoughts on... data bloat
Random thoughts on... data bloat
 
Data structure
Data structureData structure
Data structure
 
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
 
Data structure power point presentation
Data structure power point presentation Data structure power point presentation
Data structure power point presentation
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, Queue
 
Data structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithmsData structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithms
 

Similar to CS151 FIle Input and Output

Lecture02
Lecture02Lecture02
Lecture02
Rudy Martinez
 
Lecture03
Lecture03Lecture03
Lecture03
Rudy Martinez
 
CS 151 Midterm review
CS 151 Midterm reviewCS 151 Midterm review
CS 151 Midterm review
Rudy Martinez
 
CS 151 CSV output
CS 151 CSV outputCS 151 CSV output
CS 151 CSV output
Rudy Martinez
 
Blueprints: Introduction to Python programming
Blueprints: Introduction to Python programmingBlueprints: Introduction to Python programming
Blueprints: Introduction to Python programming
Bhalaji Nagarajan
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Python bible
Python biblePython bible
Python bible
adarsh j
 
CS151 Functions lecture
CS151 Functions lectureCS151 Functions lecture
CS151 Functions lecture
Rudy Martinez
 
Data Science Using Python.pptx
Data Science Using Python.pptxData Science Using Python.pptx
Data Science Using Python.pptx
Sarkunavathi Aribal
 
How to build TiDB
How to build TiDBHow to build TiDB
How to build TiDB
PingCAP
 
Python : Data Types
Python : Data TypesPython : Data Types

Similar to CS151 FIle Input and Output (11)

Lecture02
Lecture02Lecture02
Lecture02
 
Lecture03
Lecture03Lecture03
Lecture03
 
CS 151 Midterm review
CS 151 Midterm reviewCS 151 Midterm review
CS 151 Midterm review
 
CS 151 CSV output
CS 151 CSV outputCS 151 CSV output
CS 151 CSV output
 
Blueprints: Introduction to Python programming
Blueprints: Introduction to Python programmingBlueprints: Introduction to Python programming
Blueprints: Introduction to Python programming
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Python bible
Python biblePython bible
Python bible
 
CS151 Functions lecture
CS151 Functions lectureCS151 Functions lecture
CS151 Functions lecture
 
Data Science Using Python.pptx
Data Science Using Python.pptxData Science Using Python.pptx
Data Science Using Python.pptx
 
How to build TiDB
How to build TiDBHow to build TiDB
How to build TiDB
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 

More from Rudy Martinez

CS 151Exploration of python
CS 151Exploration of pythonCS 151Exploration of python
CS 151Exploration of python
Rudy Martinez
 
CS 151 Graphing lecture
CS 151 Graphing lectureCS 151 Graphing lecture
CS 151 Graphing lecture
Rudy Martinez
 
CS 151 Classes lecture 2
CS 151 Classes lecture 2CS 151 Classes lecture 2
CS 151 Classes lecture 2
Rudy Martinez
 
CS 151 Classes lecture
CS 151 Classes lectureCS 151 Classes lecture
CS 151 Classes lecture
Rudy Martinez
 
CS151 Deep copy
CS151 Deep copyCS151 Deep copy
CS151 Deep copy
Rudy Martinez
 
CS 151 Standard deviation lecture
CS 151 Standard deviation lectureCS 151 Standard deviation lecture
CS 151 Standard deviation lecture
Rudy Martinez
 
Cs 151 dictionary writer
Cs 151 dictionary writerCs 151 dictionary writer
Cs 151 dictionary writer
Rudy Martinez
 
CS 151 homework2a
CS 151 homework2aCS 151 homework2a
CS 151 homework2a
Rudy Martinez
 
CS 151 dictionary objects
CS 151 dictionary objectsCS 151 dictionary objects
CS 151 dictionary objects
Rudy Martinez
 
CS 151 Date time lecture
CS 151 Date time lectureCS 151 Date time lecture
CS 151 Date time lecture
Rudy Martinez
 
Lecture4
Lecture4Lecture4
Lecture4
Rudy Martinez
 
Lecture01
Lecture01Lecture01
Lecture01
Rudy Martinez
 
Lecture01
Lecture01Lecture01
Lecture01
Rudy Martinez
 

More from Rudy Martinez (13)

CS 151Exploration of python
CS 151Exploration of pythonCS 151Exploration of python
CS 151Exploration of python
 
CS 151 Graphing lecture
CS 151 Graphing lectureCS 151 Graphing lecture
CS 151 Graphing lecture
 
CS 151 Classes lecture 2
CS 151 Classes lecture 2CS 151 Classes lecture 2
CS 151 Classes lecture 2
 
CS 151 Classes lecture
CS 151 Classes lectureCS 151 Classes lecture
CS 151 Classes lecture
 
CS151 Deep copy
CS151 Deep copyCS151 Deep copy
CS151 Deep copy
 
CS 151 Standard deviation lecture
CS 151 Standard deviation lectureCS 151 Standard deviation lecture
CS 151 Standard deviation lecture
 
Cs 151 dictionary writer
Cs 151 dictionary writerCs 151 dictionary writer
Cs 151 dictionary writer
 
CS 151 homework2a
CS 151 homework2aCS 151 homework2a
CS 151 homework2a
 
CS 151 dictionary objects
CS 151 dictionary objectsCS 151 dictionary objects
CS 151 dictionary objects
 
CS 151 Date time lecture
CS 151 Date time lectureCS 151 Date time lecture
CS 151 Date time lecture
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture01
Lecture01Lecture01
Lecture01
 
Lecture01
Lecture01Lecture01
Lecture01
 

Recently uploaded

CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 

Recently uploaded (20)

CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 

CS151 FIle Input and Output

  • 1. Computer Science 151 An introduction to the art of computing File Input and Ouptut Rudy Martinez
  • 2. CS151 Spring 2019 Opening Files in Python ● Open() function ○ Returns a file object ○ myFile = open(‘filename’, ‘mode’) ■ Mode: ● ‘r’ - Read only ● ‘w’ - write, allows you to write and edit files ○ This will overwrite an existing file! Be careful. ● ‘a’ - append will add to the end of the file. myfile=open(‘Moby_Dick_by_Mellville.txt’, ‘r’)
  • 3. CS151 Spring 2019 Using a file object myfile=open(‘Moby_Dick_by_Mellville.txt’, ‘r’) ● To read a single line. ○ myfile.readline() ■ This returns a string ● To read all lines into a list. ○ textList = myfile.readlines() ■ This returns a list
  • 4. CS151 Spring 2019 How to read all lines ● textList = myfile.readlines() ● How do we go through a list? ○ For loop! # Open file myfile=open('/home/rudym/Downloads/Moby_Dick_by_Melville.txt', 'r') # Load each line in file to list textList textList = myfile.readlines() # For loop to read and print each line for i in range(0, len(textList), 1): print(textList[i])
  • 5. CS151 Spring 2019 Writing Files ● Writing files is similar to reading files. ● The main misstep is using ‘w’ instead of ‘a’ when you have a file you want to add to. ○ ‘w’ will write over any file ○ ‘a’ will append to the end of the file preserving previous entries outfile = open(‘Filename’, ‘w’)
  • 6. CS151 Spring 2019 Writing files ● Also like read, there are two main functions for putting text into a file. ○ write(‘string’) - will write a passed string into the file and does not append a carriage return ○ writelines( string, list, or dictionary ) - takes anything that can be iterated ■ Iterables are strings, list and dictionaries outfile = open(‘test.txt’, ‘w’) for i in range(0, 10, 1): outfile.write(str(i)) Output: 0123456789 outfile = open(‘test.txt’, ‘w’) myString = ‘Hello Cruel World’ outfile.writelines(myString) Output: Hello Cruel World
  • 7. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [H]
  • 8. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [He]
  • 9. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [Hel]
  • 10. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [Hell]
  • 11. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [Hello]
  • 12. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [Hello ]
  • 13. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [Hello C]
  • 14. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [Hello Cr]
  • 15. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [Hello Cru]
  • 16. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [Hello Crue]
  • 17. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [Hello Cruel]
  • 18. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [Hello Cruel ]
  • 19. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [Hello Cruel W]
  • 20. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [Hello Cruel Wo]
  • 21. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [Hello Cruel Wor]
  • 22. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [Hello Cruel Worl]
  • 23. CS151 Spring 2019 Iterables ● An iterable is any data structure that has an iterator ● What is an iterator? ○ An iterator is a pointer that walks any data structure. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 myString H e l l o C r u e l W o r l d [Hello Cruel World]
  • 24. CS151 Spring 2019 For loops revisited! Old For loop for i in range(0, len(myString), 1): print(myString[i]) Quick Loop for i in myString: print(i] The two for loops below do the exact same thing, but one is short hand! Why didn’t you teach us this first?!?*?
  • 25. CS151 Spring 2019 To Sum it up ● Open returns a file object for reading and writing. ● Reading: ○ readline() - Single line of text ○ readlines() - All lines into list ● Writing: ○ writeline() - Single string to file ○ writelines() - An iterable to the file
  • 26. CS151 Spring 2019 Friday ● 2/15 Homework 2 part A assigned ● 2/20 Homework 2 part A DUE! (5 days) ● 2/22 Homework 2 part B assigned ● 2/27 Homework 2 part B DUE! (5 days) ● 3/1 Homework 2 part C assigned ● 3/4 Exam Review!!! ● 3/6 Midterm Exam ● 3/29 Homework 2 part C DUE! (nearly a month)