Page 1 of 3 Files are fundamental to computing but we often take them for granted. Find out how to understand what they are and do in this extract from Programmer's Python: Everything is Data.
Programmer's Python Everything is Data
Is now available as a print book: Amazon
Contents
- Python – A Lightning Tour
- The Basic Data Type – Numbers
Extract: Bignum
- Truthy & Falsey
- Dates & Times
Extract Naive Dates
- Sequences, Lists & Tuples
Extract Sequences
- Strings
Extract Unicode Strings
- Regular Expressions
Extract Simple Regular Expressions
- The Dictionary
Extract The Dictionary
- Iterables, Sets & Generators
Extract Iterables
- Comprehensions
Extract Comprehensions
- Data Structures & Collections
Extract Stacks, Queues and Deques Extract Named Tuples and Counters
- Bits & Bit Manipulation
Extract Bits and BigNum
- Bytes
Extract Bytes And Strings Extract Byte Manipulation
- Binary Files
Extract Files and Paths ***NEW!!!
- Text Files
- Creating Custom Data Classes
Extract A Custom Data Class
- Python and Native Code
Extract Native Code Appendix I Python in Visual Studio Code Appendix II C Programming Using Visual Studio Code
<ASIN:1871962765>
<ASIN:1871962749>
<ASIN:1871962595>
<ASIN:B0CK71TQ17>
<ASIN:187196265X>
Reading and writing files is a very basic activity in any programming language and Python does the job very well. However, to understand working with files you are better off with a clear idea of bits, bytes and representations as discussed in the previous chapters. This the reason that we have postponed discussing this very commonly used feature until now. Files are a fundamental data type, but they only make good sense after you have mastered bits, bytes, encodings and more.
What is a File?
A file is a very commonly encountered data structure, so much so that we tend to take it for granted. The idea of a file arose originally because of the use of magnetic tape, a low-cost storage medium, but once invented the idea generalizes to a very wide range of situations where data makes its way into or out of the computer.
The basic idea is that a file is a source or sink of data. At its simplest it behaves like a paper or magnetic tape. There is a current position on the tape where data can be read or written.
When data is written to the file the position moves to the next free location:

When data is read from the file the position moves to the next item of data.

Most of the time you can ignore the position and allow it to be automatically adjusted as you use the file.
What all this means is that you can think in terms of writing one data item after another until you are finished. Then you can “re-open” the file which “re-winds” the tape or positions the pointer to the start of the file. When you read the file you read items in the order that they were written to the file. In this sense a file is like First In First Out stack, i.e a queue. When you are writing to the file the pointer is the end of the queue. When you are reading the pointer is the front of the queue.
It also helps to think in terms of opening and closing files. Opening a file determines which file you are using and sets the pointer to the starting position. Closing a file returns it to wherever it lives and makes sure that any writing operations are completed. You can only work with a file when it is open.
Although files started out as being stored on tape, today files are very general. Indeed Unix and Linux embody the idea that every external data source or sink is a file – a keyboard is a file you read from, a monitor is a file you write to, and so on. In the same way, a file can be used to send and receive data over a communications channel such as an internet or inter-process socket.
Sometimes this idea fits very well and sometimes it has to be forced a little. What all this means is that opening, closing, reading and writing files constitute a very general approach to getting data into and out of the computer.
|