Programmer's Python Data - Files and Paths
Written by Mike James   
Monday, 17 February 2025
Article Index
Programmer's Python Data - Files and Paths
Opening Files
Path

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

pythondata360Contents

  1. Python – A Lightning Tour
  2. The Basic Data Type – Numbers
       Extract: Bignum
  3. Truthy & Falsey
  4. Dates & Times
       Extract Naive Dates
  5. Sequences, Lists & Tuples
       Extract Sequences 
  6. Strings
       Extract Unicode Strings
  7. Regular Expressions
       Extract Simple Regular Expressions 
  8. The Dictionary
       Extract The Dictionary 
  9. Iterables, Sets & Generators
       Extract  Iterables 
  10. Comprehensions
       Extract  Comprehensions 
  11. Data Structures & Collections
       Extract Stacks, Queues and Deques
      
    Extract Named Tuples and Counters
  12. Bits & Bit Manipulation
       Extract Bits and BigNum 
  13. Bytes
       Extract Bytes And Strings
       Extract Byte Manipulation 
  14. Binary Files
       Extract Files and Paths ***NEW!!!
  15. Text Files
  16. Creating Custom Data Classes
        Extract A Custom Data Class 
  17. 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:

file1


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

file2

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.



Last Updated ( Monday, 17 February 2025 )