Files Handling In today’s lesson we will look at:

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

Physical DataBase Design
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Computer Science 111 Fundamentals of Programming I Files.
Chapter 3 – Computer Hardware Computer Components – Hardware (cont.) Lecture 3.
Working with Files CSC 161: The Art of Programming Prof. Henry Kautz 11/9/2009.
Memory. Random Access Memory Defined What is memory? operating system and other system software that control the usage of the computer equipment application.
11:15:01 Storage device. Computer memory Primary storage 11:15:01.
Computing Hardware Starter.
Lecture 16 – Open, read, write and close files.  At the end of this lecture, students should be able to:  understand file structure  open and close.
Lists in Python.
MAC OS – Unit A Page: 10-11, Investigating Data Processing Understanding Memory.
Computer memory. Bits and bytes  Data can be stored and measured in bytes  One bytes can contains 8 bytes  A bits can only be 0 or 1  A series of.
Data and its manifestations. Storage and Retrieval techniques.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
External data structures
Search. Search issues How do we say what we want? –I want a story about pigs –I want a picture of a rooster –How many televisions were sold in Vietnam.
FILE HANDLING IN C++.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Storing and Retrieving Data
5 1 Data Files CGI/Perl Programming By Diane Zak.
GCSE ICT Storing data - Internal memory, backing storage, and measuring memory.
UoS Libraries 2011 EndNote X5 - basic graduate session.
© GCSE Computing Computing Hardware Starter. Creating a spreadsheet to demonstrate the size of memory. 1 byte = 1 character or about 1 pixel of information.
Files Tutor: You will need ….
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
CSV Files Intro to Computer Science CS1510 Dr. Sarah Diesburg.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
GCSE ICT Data Transfer. Data transfer Users often need to transfer data between software packages or computers. Until relatively recently this was difficult.
Computer Graphics HARDWARE. Computers  Computers are automatic, electronic machines that –accept data & instructions from a user (INPUT) –store the data.
GCSE ICT Data Transfer.
COMPSCI 107 Computer Science Fundamentals
Topic: File Input/Output (I/O)
Fundamentals of Python: First Programs
Containers and Lists CIS 40 – Introduction to Programming in Python
Python’s input and output
Binary Files.
Data File Import / Export
Introduction to Computers
Data Storage In today’s lesson we will look at:
Chapter 13: File Input and Output
CS190/295 Programming in Python for Life Sciences: Lecture 1
FILE HANDLING IN C++.
Disk Storage, Basic File Structures, and Buffer Management
Python I/O.
Seek Method and CSV Files
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
Fundamentals of Programming I Files
Learning to Program in Python
Data Structures – 1D Lists
Seek Method and CSV Files
Fundamentals of Data Structures
Programming Training Main Points: - Working with strings.
2.C Memory GCSE Computing Langley Park School for Boys.
15-110: Principles of Computing
Topics Introduction to File Input and Output
AS Level ICT Selection and use of storage requirements, media, and devices: storage and storage capacity Unit 1 Topic a - Selection and use of storage.
Topics Introduction to File Input and Output
Nate Brunelle Today: Web Reading
File handling in C By Anand George.
Introduction to Computer Science
Presentation transcript:

Files Handling In today’s lesson we will look at: the need for permanent storage methods for persistent storage of data methods for reading from and writing to files

Types of Storage Primary storage (memory/RAM): Volatile - i.e. contents are lost without power Use to store data while it is being processed Fast - access time measured in nanoseconds for PCs Relatively expensive - 1Gb DDR RAM costs about £20 Secondary storage (e.g. hard disc): Non-volatile, usually magnetic media - data are stored without power Used for permanent storage of data after it has been “saved” Slower - access time of around 8ms Cheaper, e.g. 1Tb for £50

Storing Data When writing a program, we use variables to store data in primary storage. What sort of things might we want to store permanently? previous activity – e.g. high scores, data collected from sales, experiments, etc. reference materials – e.g. dictionaries for spelling checkers, lists for validation, television program guides, address lists, etc.

Permanent Storage How can a computer store data permanently? databases: the best way to store large amounts of data, especially if you will want to sort or search through it cookies: web-pages aren’t allowed to access your normal files for security reasons, but they can store small amounts of data in special files called cookies. files: programs can store data in files that are visible to your operating system – either in an unstructured way for small amounts of data, or using a format such as CSV or XML.

Access Files in Python The first stage in handling file contents in Python is to open the file. There is actually a file variable type in Python that we can use to point to a file, e.g. f = open(“my_file.txt”, “r”) will open a file called my_file.txt. The second part is called the mode tells Python how to access the file – e.g. “r” means open the file read-only.

File Access Modes The most common file access modes are: “r” = read only “w” = write to the file (and replace the contents) “a” = append (i.e. add to the end of the file) You can also add a + to the end of the mode if you want to be able to update the file, e.g. f = open(“my_file.txt”, “r+”) Files should be closed when you have finished, e.g. using the command f.close()

Writing to Files If you open a file with mode w or a then it will be created if it doesn’t already exist. You can store a string in a file using the write() method, e.g. f.open(“my_file.txt”, “w”) f.write(“Hello”) f.close() You can write the contents of a variable, but if it’s not a string it will need to be converted first using str().

Reading the Whole File You can read the whole file into a list, e.g. f.open(“my_file.txt”, “r”) contents = f.read() f.close() This will create a string called contents containing every character in the file. You can split the string using the .split() method, e.g. for a CSV file you could use contents.split(“,”) to separate the string into individual values at the commas.

Reading the File into a List You can read the whole file into a list, e.g. f.open(“my_file.txt”, “r”) contents = f.readlines() f.close() This will create a list called contents containing each line from the file as an item in the list. The newline characters will also be read from the file and will appear in the list as \n. You can remove these characters using the strip() method, e.g. contents[0].strip()

Reading a Single Line Alternatively, you can read individual lines from the file into a string using readline(). If you reach the end of the file, readline() returns an empty string (i.e. nothing, or “”), so you can do things like this: f = open('my_file.txt','r') item = True while item: item = f.readline() print item.strip() f.close()

Further Reading... These are only the basic and most common things that you might want to do with files. You can also, for example: store non-text data (e.g. for images or sound) in files; this is called binary mode jump to a certain position inside a file, e.g. to insert data at a particular point look where you are in a file – e.g. a numbered byte read a single byte from a position in a file use pickle to store data structures (e.g. lists) in text files