SlideShare a Scribd company logo
1 of 13
Download to read offline
File I/O	

Python Built-in Data Type
                                       NCCU Computer Science Dept.
                            Python Programming for Non-Programmer
Recall & Warn Up
name = open(‘filename’, ‘w’)
read()
readline()
readlines()
write()
writelines()
                         File I/O
                         Introduction
Get The Source


• goo.gl/18AIo
• Finish the code by yourself!

                                 File I/O
                                 Introduction
Declaration
def load_numbers(numbers, filename):
    pass



def save_numbers(numbers, filename):
    pass




                                       File I/O
                                       Introduction
Declaration
def load_numbers(numbers, filename):
    pass



def save_numbers(numbers, filename):
    pass




         Fill them!
                                       File I/O
                                       Introduction
Two New Functions
• Load Numbers
        Variable   File

• Save Numbers
        Variable   File

                          File I/O
                          Introduction
File Format
Should be just like below....


          The President of Taiwan, 0912345678
          Google Incorporate, 02-12345678
          The President of NCCU, 02-29393091
          The Queen of NCCU dogs, xxxxxx
          .....



                                                File I/O
                                                Introduction
Load Numbers
First, declare the file object and
  the close method of the file

def load_numbers(numbers, filename):
    in_file = open(filename, "rt")

    # do something...

    in_file.close()




                                       File I/O
                                       Introduction
Load Numbers
And make a while loop for reading lines of file

       def load_numbers(numbers, filename):
           in_file = open(filename, "rt")

           while True:
               in_line = in_file.readline()
               if not in_line:
                   break

           in_file.close()



                                              File I/O
                                              Introduction
Load Numbers
     Remove the last character of newline(“n”)

def load_numbers(numbers, filename):
    in_file = open(filename, "rt")     “The stringn”

    while True:
        in_line = in_file.readline()
        if not in_line:
            break
        in_line = in_line[:-1]          “The string”
    in_file.close()


                                              File I/O
                                              Introduction
Load Numbers
def load_numbers(numbers, filename):
    in_file = open(filename, "r")

    while True:
        in_line = in_file.readline()
        if not in_line:
            break
        in_line = in_line[:-1]

        name, number = in_line.split(‘,’) # split to two parts
        numbers[name] = number            # save them to numbers

    in_file.close()

                                                      File I/O
                                                      Introduction
Save Numbers
    def save_numbers(numbers, filename):
        out_file = open(filename, "w")

         for k, v in numbers.items():
             out_file.write(k + "," + v + "n")

         out_file.close()




Multiple assignment:
   (k, v) = [“name”, “phone”]
   >>> k
    “name”
   >>> v
    “phone”                                       File I/O
                                                  Introduction
Finish


• Finish Code: goo.gl/DDK7d
• Practice by yourself

                              File I/O
                              Introduction

More Related Content

What's hot

UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CAshim Lamichhane
 
19 structured files
19 structured files19 structured files
19 structured filesashish61_scs
 
File Management in C
File Management in CFile Management in C
File Management in CPaurav Shah
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10patcha535
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Rex Joe
 
python file handling
python file handlingpython file handling
python file handlingjhona2z
 
Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Abdullah khawar
 
File and directories in python
File and directories in pythonFile and directories in python
File and directories in pythonLifna C.S
 
C programming disk file reading and writing
C programming disk file reading and writingC programming disk file reading and writing
C programming disk file reading and writingrishi ram khanal
 
file handling c++
file handling c++file handling c++
file handling c++Guddu Spy
 
File handling(some slides only)
File handling(some slides only)File handling(some slides only)
File handling(some slides only)Kamlesh Nishad
 
Files in C
Files in CFiles in C
Files in CPrabu U
 

What's hot (17)

UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
19 structured files
19 structured files19 structured files
19 structured files
 
File Management in C
File Management in CFile Management in C
File Management in C
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
 
Hashes Master
Hashes MasterHashes Master
Hashes Master
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
Unit5
Unit5Unit5
Unit5
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
python file handling
python file handlingpython file handling
python file handling
 
Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)
 
File and directories in python
File and directories in pythonFile and directories in python
File and directories in python
 
C programming disk file reading and writing
C programming disk file reading and writingC programming disk file reading and writing
C programming disk file reading and writing
 
file handling c++
file handling c++file handling c++
file handling c++
 
File handling(some slides only)
File handling(some slides only)File handling(some slides only)
File handling(some slides only)
 
Files in C
Files in CFiles in C
Files in C
 

Viewers also liked

Vim editor
Vim editorVim editor
Vim editorColin Su
 
C:\fakepath\velence igy
C:\fakepath\velence igyC:\fakepath\velence igy
C:\fakepath\velence igySzollos
 
Introduction to Google App Engine
Introduction to Google App EngineIntroduction to Google App Engine
Introduction to Google App EngineColin Su
 
доклад роль сетевых структур
доклад роль сетевых структурдоклад роль сетевых структур
доклад роль сетевых структурEXPERT0764
 
Microsoft SQL server - complete BI platform by Alexander Gacz
Microsoft SQL server - complete BI platform by Alexander GaczMicrosoft SQL server - complete BI platform by Alexander Gacz
Microsoft SQL server - complete BI platform by Alexander GaczMark Tapley
 
Django Deployer
Django DeployerDjango Deployer
Django DeployerColin Su
 

Viewers also liked (6)

Vim editor
Vim editorVim editor
Vim editor
 
C:\fakepath\velence igy
C:\fakepath\velence igyC:\fakepath\velence igy
C:\fakepath\velence igy
 
Introduction to Google App Engine
Introduction to Google App EngineIntroduction to Google App Engine
Introduction to Google App Engine
 
доклад роль сетевых структур
доклад роль сетевых структурдоклад роль сетевых структур
доклад роль сетевых структур
 
Microsoft SQL server - complete BI platform by Alexander Gacz
Microsoft SQL server - complete BI platform by Alexander GaczMicrosoft SQL server - complete BI platform by Alexander Gacz
Microsoft SQL server - complete BI platform by Alexander Gacz
 
Django Deployer
Django DeployerDjango Deployer
Django Deployer
 

Similar to Python-FileIO

Similar to Python-FileIO (20)

Unit VI
Unit VI Unit VI
Unit VI
 
FILE HANDLING.pptx
FILE HANDLING.pptxFILE HANDLING.pptx
FILE HANDLING.pptx
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
file handling.pdf
file handling.pdffile handling.pdf
file handling.pdf
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
Module2-Files.pdf
Module2-Files.pdfModule2-Files.pdf
Module2-Files.pdf
 
Python - Lecture 9
Python - Lecture 9Python - Lecture 9
Python - Lecture 9
 
Python File functions
Python File functionsPython File functions
Python File functions
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
2Bytesprog2 course_2014_c3_txtfiles
2Bytesprog2 course_2014_c3_txtfiles2Bytesprog2 course_2014_c3_txtfiles
2Bytesprog2 course_2014_c3_txtfiles
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file
CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary fileCBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file
CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file
 
File handling in c
File  handling in cFile  handling in c
File handling in c
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
filecalcpy filecalcfunctionpy quickbug_datatxt 3 Erro.pdf
filecalcpy filecalcfunctionpy quickbug_datatxt 3 Erro.pdffilecalcpy filecalcfunctionpy quickbug_datatxt 3 Erro.pdf
filecalcpy filecalcfunctionpy quickbug_datatxt 3 Erro.pdf
 
File-Data structure
File-Data structure File-Data structure
File-Data structure
 
Files in c++
Files in c++Files in c++
Files in c++
 
C, C++ Training Institute in Chennai , Adyar
C, C++ Training Institute in Chennai , AdyarC, C++ Training Institute in Chennai , Adyar
C, C++ Training Institute in Chennai , Adyar
 

More from Colin Su

Introduction to Google Compute Engine
Introduction to Google Compute EngineIntroduction to Google Compute Engine
Introduction to Google Compute EngineColin Su
 
Introduction to Google Cloud Endpoints: Speed Up Your API Development
Introduction to Google Cloud Endpoints: Speed Up Your API DevelopmentIntroduction to Google Cloud Endpoints: Speed Up Your API Development
Introduction to Google Cloud Endpoints: Speed Up Your API DevelopmentColin Su
 
Functional programming in Python
Functional programming in PythonFunctional programming in Python
Functional programming in PythonColin Su
 
Web2py Code Lab
Web2py Code LabWeb2py Code Lab
Web2py Code LabColin Su
 
A Tour of Google Cloud Platform
A Tour of Google Cloud PlatformA Tour of Google Cloud Platform
A Tour of Google Cloud PlatformColin Su
 
Introduction to Facebook JavaScript & Python SDK
Introduction to Facebook JavaScript & Python SDKIntroduction to Facebook JavaScript & Python SDK
Introduction to Facebook JavaScript & Python SDKColin Su
 
Introduction to MapReduce & hadoop
Introduction to MapReduce & hadoopIntroduction to MapReduce & hadoop
Introduction to MapReduce & hadoopColin Su
 
Introduction to Google - the most natural way to learn English (English Speech)
Introduction to Google - the most natural way to learn English (English Speech)Introduction to Google - the most natural way to learn English (English Speech)
Introduction to Google - the most natural way to learn English (English Speech)Colin Su
 
How to Speak Charms Like a Wizard
How to Speak Charms Like a WizardHow to Speak Charms Like a Wizard
How to Speak Charms Like a WizardColin Su
 
房地產報告
房地產報告房地產報告
房地產報告Colin Su
 
Introduction to Git
Introduction to GitIntroduction to Git
Introduction to GitColin Su
 
Introduction to Facebook Javascript SDK (NEW)
Introduction to Facebook Javascript SDK (NEW)Introduction to Facebook Javascript SDK (NEW)
Introduction to Facebook Javascript SDK (NEW)Colin Su
 
Introduction to Facebook Python API
Introduction to Facebook Python APIIntroduction to Facebook Python API
Introduction to Facebook Python APIColin Su
 
Facebook Python SDK - Introduction
Facebook Python SDK - IntroductionFacebook Python SDK - Introduction
Facebook Python SDK - IntroductionColin Su
 
Web Programming - 1st TA Session
Web Programming - 1st TA SessionWeb Programming - 1st TA Session
Web Programming - 1st TA SessionColin Su
 
Nested List Comprehension and Binary Search
Nested List Comprehension and Binary SearchNested List Comprehension and Binary Search
Nested List Comprehension and Binary SearchColin Su
 
Python-List comprehension
Python-List comprehensionPython-List comprehension
Python-List comprehensionColin Su
 
Python Dictionary
Python DictionaryPython Dictionary
Python DictionaryColin Su
 
VPython introduction
VPython introductionVPython introduction
VPython introductionColin Su
 
Linux-Permission
Linux-PermissionLinux-Permission
Linux-PermissionColin Su
 

More from Colin Su (20)

Introduction to Google Compute Engine
Introduction to Google Compute EngineIntroduction to Google Compute Engine
Introduction to Google Compute Engine
 
Introduction to Google Cloud Endpoints: Speed Up Your API Development
Introduction to Google Cloud Endpoints: Speed Up Your API DevelopmentIntroduction to Google Cloud Endpoints: Speed Up Your API Development
Introduction to Google Cloud Endpoints: Speed Up Your API Development
 
Functional programming in Python
Functional programming in PythonFunctional programming in Python
Functional programming in Python
 
Web2py Code Lab
Web2py Code LabWeb2py Code Lab
Web2py Code Lab
 
A Tour of Google Cloud Platform
A Tour of Google Cloud PlatformA Tour of Google Cloud Platform
A Tour of Google Cloud Platform
 
Introduction to Facebook JavaScript & Python SDK
Introduction to Facebook JavaScript & Python SDKIntroduction to Facebook JavaScript & Python SDK
Introduction to Facebook JavaScript & Python SDK
 
Introduction to MapReduce & hadoop
Introduction to MapReduce & hadoopIntroduction to MapReduce & hadoop
Introduction to MapReduce & hadoop
 
Introduction to Google - the most natural way to learn English (English Speech)
Introduction to Google - the most natural way to learn English (English Speech)Introduction to Google - the most natural way to learn English (English Speech)
Introduction to Google - the most natural way to learn English (English Speech)
 
How to Speak Charms Like a Wizard
How to Speak Charms Like a WizardHow to Speak Charms Like a Wizard
How to Speak Charms Like a Wizard
 
房地產報告
房地產報告房地產報告
房地產報告
 
Introduction to Git
Introduction to GitIntroduction to Git
Introduction to Git
 
Introduction to Facebook Javascript SDK (NEW)
Introduction to Facebook Javascript SDK (NEW)Introduction to Facebook Javascript SDK (NEW)
Introduction to Facebook Javascript SDK (NEW)
 
Introduction to Facebook Python API
Introduction to Facebook Python APIIntroduction to Facebook Python API
Introduction to Facebook Python API
 
Facebook Python SDK - Introduction
Facebook Python SDK - IntroductionFacebook Python SDK - Introduction
Facebook Python SDK - Introduction
 
Web Programming - 1st TA Session
Web Programming - 1st TA SessionWeb Programming - 1st TA Session
Web Programming - 1st TA Session
 
Nested List Comprehension and Binary Search
Nested List Comprehension and Binary SearchNested List Comprehension and Binary Search
Nested List Comprehension and Binary Search
 
Python-List comprehension
Python-List comprehensionPython-List comprehension
Python-List comprehension
 
Python Dictionary
Python DictionaryPython Dictionary
Python Dictionary
 
VPython introduction
VPython introductionVPython introduction
VPython introduction
 
Linux-Permission
Linux-PermissionLinux-Permission
Linux-Permission
 

Python-FileIO

  • 1. File I/O Python Built-in Data Type NCCU Computer Science Dept. Python Programming for Non-Programmer
  • 2. Recall & Warn Up name = open(‘filename’, ‘w’) read() readline() readlines() write() writelines() File I/O Introduction
  • 3. Get The Source • goo.gl/18AIo • Finish the code by yourself! File I/O Introduction
  • 4. Declaration def load_numbers(numbers, filename): pass def save_numbers(numbers, filename): pass File I/O Introduction
  • 5. Declaration def load_numbers(numbers, filename): pass def save_numbers(numbers, filename): pass Fill them! File I/O Introduction
  • 6. Two New Functions • Load Numbers Variable File • Save Numbers Variable File File I/O Introduction
  • 7. File Format Should be just like below.... The President of Taiwan, 0912345678 Google Incorporate, 02-12345678 The President of NCCU, 02-29393091 The Queen of NCCU dogs, xxxxxx ..... File I/O Introduction
  • 8. Load Numbers First, declare the file object and the close method of the file def load_numbers(numbers, filename): in_file = open(filename, "rt") # do something... in_file.close() File I/O Introduction
  • 9. Load Numbers And make a while loop for reading lines of file def load_numbers(numbers, filename): in_file = open(filename, "rt") while True: in_line = in_file.readline() if not in_line: break in_file.close() File I/O Introduction
  • 10. Load Numbers Remove the last character of newline(“n”) def load_numbers(numbers, filename): in_file = open(filename, "rt") “The stringn” while True: in_line = in_file.readline() if not in_line: break in_line = in_line[:-1] “The string” in_file.close() File I/O Introduction
  • 11. Load Numbers def load_numbers(numbers, filename): in_file = open(filename, "r") while True: in_line = in_file.readline() if not in_line: break in_line = in_line[:-1] name, number = in_line.split(‘,’) # split to two parts numbers[name] = number # save them to numbers in_file.close() File I/O Introduction
  • 12. Save Numbers def save_numbers(numbers, filename): out_file = open(filename, "w") for k, v in numbers.items(): out_file.write(k + "," + v + "n") out_file.close() Multiple assignment: (k, v) = [“name”, “phone”] >>> k “name” >>> v “phone” File I/O Introduction
  • 13. Finish • Finish Code: goo.gl/DDK7d • Practice by yourself File I/O Introduction

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n