SlideShare a Scribd company logo
1 of 26
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 structureZaid Shabbir
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure pptNalinNishant3
 
Algorithm and Data Structure - Queue
Algorithm and Data Structure - QueueAlgorithm and Data Structure - Queue
Algorithm and Data Structure - QueueAndiNurkholis1
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structurefaran nawaz
 
basics of queues
basics of queuesbasics of queues
basics of queuessirmanohar
 
3 searching algorithms in Java
3 searching algorithms in Java3 searching algorithms in Java
3 searching algorithms in JavaMahmoud Alfarra
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURERohit Rai
 
Algorithm and Data Structure - Stack
Algorithm and Data Structure - StackAlgorithm and Data Structure - Stack
Algorithm and Data Structure - StackAndiNurkholis1
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structureVivek Kumar Sinha
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its typesRameesha Sadaqat
 
Random thoughts on... data bloat
Random thoughts on... data bloatRandom thoughts on... data bloat
Random thoughts on... data bloatNigel 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 algorithmsAbimbola 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

Similar to CS151 FIle Input and Output (10)

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
 
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 pythonRudy Martinez
 
CS 151 Graphing lecture
CS 151 Graphing lectureCS 151 Graphing lecture
CS 151 Graphing lectureRudy Martinez
 
CS 151 Classes lecture 2
CS 151 Classes lecture 2CS 151 Classes lecture 2
CS 151 Classes lecture 2Rudy Martinez
 
CS 151 Classes lecture
CS 151 Classes lectureCS 151 Classes lecture
CS 151 Classes lectureRudy Martinez
 
CS 151 Standard deviation lecture
CS 151 Standard deviation lectureCS 151 Standard deviation lecture
CS 151 Standard deviation lectureRudy Martinez
 
Cs 151 dictionary writer
Cs 151 dictionary writerCs 151 dictionary writer
Cs 151 dictionary writerRudy Martinez
 
CS 151 dictionary objects
CS 151 dictionary objectsCS 151 dictionary objects
CS 151 dictionary objectsRudy Martinez
 
CS 151 Date time lecture
CS 151 Date time lectureCS 151 Date time lecture
CS 151 Date time lectureRudy 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

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 

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)