1 / 6

Files in 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 them More than this, you need an external file stored on secondary storage. External data files. Handles large amounts of data

deana
Download Presentation

Files in Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Files in Python The Basics

  2. Why use Files? • Very small amounts of data – just hardcode them into the program • A few pieces of data – ask the user to input them • More than this, you need an external file stored on secondary storage

  3. External data files • Handles large amounts of data • Data is independent of program, so program can change without changing data • Easier to edit data in an editor, instead of during run of program (can’t go back!) • Use the same data for input to different programs • Output files can be saved for later use • Output of one program can be used for input of another

  4. Text files versus Binary files • Text files created by editors, stored as ASCII codes • Binary files stored as raw binary numbers, have to be handled differently • Text files are manipulated sequentially only • Binary files can be manipulated sequentially or randomly (we will not do binary files in this class)

  5. Creating a text data file • This is done just like creating any other text file • You can use Notepad • You can use the editors of the IDEs that create Python • You can use a word processor like Word if you are careful to save as plain text • Store the text file in the same folder as you put your source code

  6. Delimiters • The \n (newline) (carriage return) is a very important symbol in text files. • It delimits what Python calls a ‘line’ in the file. • It gets put into the file whenever you press Enter at the end of a line • A blank line is represented by two newlines together \n\n • It matters whether you press Enter at the end of the last line of the file – some methods in Python will treat the last line differently because of the \n character

More Related