text
stringlengths
0
897
There are two reasons why object-oriented programming has become so popular in the last few decades. One is that it helps keep big programs neat and organized, with all the functions for a particular kind of thing bundled together into classes. The other is that it makes it easier to reuse code.
Code reuse is kind of the holy grail of programming. Faced with a need — say, drawing a little picture on the screen that can efficiently move, rotate, and change size — you only want to write code for that once, at most. Then the *next* time you face that same need, you want to just pull that code off the shelf and ...
Conversely, with OOP, reusing code like that is substantially easier. If you have a little picture-moving class — let's call it Sprite — then everything there is to managing sprites can be wrapped up in that class. You can add that class to your program, and as long as you don't already have something called Sprite, ...
Of course you could get *that* benefit just by stuffing a bunch of related methods into a map, without using any of the OOP features like `new` and `self`. And in some cases that's all you need. But when those methods operate on some cluster of data, like the block example above or the position, rotation, and size of...
As a new programmer, you may not see lots of opportunities to define your own classes right away, and that's OK. You will, however, start *using* classes a lot after Chapter 14, because that's when we get into Mini Micro. Mini Micro has a built-in Sprite class, along with other classes that represent sounds, files, d...
A> **Chapter Review**
A> - You learned about object-oriented programming (OOP), and how to do it in MiniScript using `new` and `self`.
A> - You began thinking about how to write code that can be reused later (your future self will thank you).
{chapterHead: "Day 13: Working with Files", startingPageNum:139}
{width: "50%"}
![](Chapter13.svg)
Q> Knowledge is power.
Q>— Francis Bacon (1561 - 1626)
A> **Chapter Objectives**
A> - Learn the command-line MiniScript functions for working with disk files.
A> - Start using MiniScript as a superpower that lets you automate repetitive tasks.
After twelve days of MiniScript, you now know all the essential points of the MiniScript language, as well as the intrinsic functions that are common to all MiniScript environments. Congratulations! That's a real achievement.
Now we can start considering the additional functionality provided by certain MiniScript environments. In Chapter 8, you installed command-line MiniScript, and hopefully you've continued to work with it for at least some of the examples since then. If not, that's OK — but you might want to go back and brush up on it,...
In addition to all the standard MiniScript functionality, command-line MiniScript provides some extra functions and classes for working with disk files. That is an extremely useful thing to do, as everything on a computer is organized into files and folders. With the powers you gain today, you'll be able to create fi...
D> The file-related functions covered in this chapter also happen to be available in Mini Micro, which you'll start using in only two more days! So while you're working with command-line MiniScript today, you're also getting a head start on Mini Micro.
If you go back to the web page where you downloaded command-line MiniScript:
_
: <https://miniscript.org/cmdline/>
...you will find the file-related functions listed there. Note that because these are not part of the MiniScript core, they are *not* on the MiniScript Quick Reference sheet, nor in the MiniScript Manual.
## Files, Folders, and Paths
Let's begin my making sure we're all clear on some basic terminology that relates to computer files.
file
: a collection of bytes stored on disk. Each file has a name (often including an extension at the end, like ".txt" or ".jpg"), the actual file content, and some other metadata like a creation date. Things like pictures, text files, and word processing documents are all files.
folder
: a special kind of disk entry that contains files and other folders. Sometimes called a *directory*.
path
: a string that uniquely identifies a file or folder location by specifying the sequence of nested folders needed to get there, as well as the file or folder name itself. Each part of the path is separated by a slash (a forward slash on Mac, Linux, and Mini Micro; a backslash on Windows).
Every desktop operating system provides some way to browse files and folders as little icons in a window. But when you're working on the command line, you work with those same files and folders using paths and file names.
Paths come in two types, absolute and relative.
absolute path
: a path that begins with a disk identifier, and lists all the folders from the very top level of that disk down to the file. On Windows, absolute paths begin with a drive letter like "C:". On other systems, absolute paths begin with a forward slash.
relative path
: a path that does not start at the top level of the disk, and so it can only be interpreted relative to some other folder. Sometimes called a *partial path*. A file name is an extreme case of a relative path.
If we were not lazy, we would probably just use absolute paths all the time, since they are unambiguous. But we are, so we use relative paths a lot. How then can the computer know how to interpret them? It does so by defining one folder in particular as the *working directory*, which is the base for any relative pat...
working directory
: the folder that partial paths are considered to be relative to. Sometimes called the *current directory* or *current folder*.
## The file module
{i:"module;`file` module"}
Command-line MiniScript (and Mini Micro) tucks away all the file-related function inside a map called `file`. That's just to avoid adding a dozen new identifiers into the global variable space; instead, it only adds one. Because this map is *not* a class intended to be used with `new`, it is not capitalized. We some...
Start by firing up command-line MiniScript, and at the REPL prompt, type:
```terminal
> file.indexes
```
Recall that the `indexes` method, when called on a map, returns a list of all the keys in the map. In this case, that tells you the names of all the functions in the file module. There are a bit over a dozen. The full set is shown below, but just like the tables of intrinsic functions in earlier chapters, don't try ...
{caption:"Functions in the `file` module", colWidths:"170,*"}
| `file.curdir` | returns the current working directory |
| `file.setdir(path)` | changes the current working directory |
| `file.makedir(path)` | creates a new directory |
| `file.children(path)` | returns list of files within the given directory |
| `file.name(path)` | returns the file name from the given path |
| `file.parent(path)` | returns the path to the directory containing the given path |
| `file.child(parentPath, childName)` | combines the given directory path and file name, returning path to the child |
| `file.exists(path)` | returns true if a file exists at the given path |
| `file.copy(oldPath, newPath)` | copies a file to a new location |
| `file.move(oldPath, newPath)` | moves/renames a file |
| `file.delete(path)` | deletes a file |
| `file.info(path)` | gets a map of details about a file including size, isDirectory, date, and (full) path |
| `file.readLines(path)` | returns full contents of a text file as a list of strings |
| `file.writeLines(path, data)` | writes out the given list as a text file, one line per list element |
| `file.open(path, mode="rw+")` | opens or creates a file |