# Introducing Julia/Working with text files
## Reading from files

The standard approach for getting information from a text file is using the open, read, and close functions.

### Open

To read text from a file, first obtain a file handle:
f is now Julia's connection to the file on disk. When you've finished with the file, you should close the connection, using:
In general, the recommended way to work with a file in Julia is to wrap any file-processing functions inside a do block:
The open file is automatically closed when this block finishes. See Controlling the flow for more about do blocks.
Because of the scope of local variables in blocks, you might want to keep some of information that was processed:

### Slurp – reading a file all at once

You can read the entire contents of an open file at once with read:
This stores the contents of the file in s:
You can use readlines to read in the whole file as an array, with each line an element:
Now you can step through the lines:
There's a better way to do this – see enumerate, below.
You might find the chomp function useful – it removes the trailing newline from a string.

### Line by line

The eachline function turns a source into an iterator. This allows you to process a file a line at a time:
Another approach is to read until you reach the end of the file. You might want to keep track of which line you're on:
A better approach is to use enumerate on an iterable object – you'll get the line numbering 'for free':
If you have a specific function that you want to call on a file, you can use this alternative syntax:
This opens the file, runs the shout function on it, then closes it again, assigning the processed contents to the variable.
You can use the CSV.jl to read and write comma-separated-values (.csv) files, and it's recommended over (handles more corner cases and can be faster, especially for larger files) using DelimitedFiles.readdlm function to read lines delimited with certain characters, such as data files, arrays stored as text files, and tables. If you use the DataFrames package, there's also a readtable specifically designed to read data into a table.

## Working with paths and filenames

These functions will be useful for working with filenames:
* cd(path) changes the current directory.
* pwd gets the current working directory.
* readdir(path) returns a lists of the contents of a named directory, or the current directory.
* abspath(path) adds the current directory's path to a filename to make an absolute pathname.
* joinpath(str, str, ...) assembles a pathname from pieces.
* isdir(path) tells you whether the path is a directory.
* splitdir(path) – split a path into a tuple of the directory name and file name.
* splitdrive(path) – on Windows, split a path into the drive letter part and the path part. On Unix systems, the first component is always the empty string.
* splitext(path) – if the last component of a path contains a dot, split the path into everything before the dot and everything including and after the dot. Otherwise, return a tuple of the argument unmodified and the empty string.
* expanduser(path) – replaces a tilde character at the start of a path with the current user's home directory.
* normpath(path) – normalizes a path, removing "." and ".." entries.
* realpath(path) – canonicalizes a path by expanding symbolic links and removing "." and ".." entries.
* homedir – gets current user's home directory.
* dirname(path) – gets the directory part of a path.
* basename(path) – gets the file name part of a path.
To work on a restricted selection of files in a directory, use filter and an anonymous function to filter the file names and just keep the ones you want. (filter is more of a fishing net or sieve, rather than a coffee filter, in that it catches what you want to keep.)
If you want to match a group of files using a regular expression, then use occursin. Let's look for files with ".jpg" or ".png" suffixes (remembering to escape the "."):
To examine a file hierarchy, use walkdir, which lets you work through a directory, and examine the files in each directory in turn.

### File information

If you want information about a specific file, use stat("pathname"), and then use one of the fields to find out the information. Here's how to get all the information and the field names listed for a file "i":
You can access these fields via a 'stat' structure:
and you can also use some of them directly:
although not size:
To work on specific files that meet conditions – all Jupyter files (i.e. files with the extension "ipynb") modified after a certain date, for example – you could use something like this:

### Interacting with the file system

The cp, mv, rm, and touch functions have the same names and functions as their Unix shell counterparts.
To convert filenames to pathnames, use abspath. You can map this over a list of files in a directory:
To restrict the list to filenames that contain a particular substring, use an anonymous function inside filter – something like this:
To restrict the list to regular expression matches, try this:

## Writing to files

To write to a text file, open it using the "w" flag and make sure that you have permission to create the file in the specified directory:
Here's how to write 20 lines of 4 random numbers between 1 and 10, separated by commas:
A quicker alternative to this is to use the DelimitedFiles.writedlm function, described next:

### Writing and reading array to and from a file

In the DelimitedFiles package are two convenient functions, writedlm and readdlm. These let you read/write an array or collection from/to a file.
writedlm writes the contents of an object to a text file, and readdlm reads the data from a file into an array:
You can see the file using the shell (type a semicolon ";" to switch):
The elements are separated by tabs unless you specify another delimiter. Here, a colon is used to delimit the numbers:
To read in data from a text file, you can use readdlm.
There are also a number of Julia packages specifically designed for reading and writing data to files, including DataFrames.jl and CSV.jl. Search in JuliaHub or JuliaPackages for these and more. Many of these packages live at the home of the JuliaData organization.