Skip to content
Snippets Groups Projects
Commit 6ca33fe9 authored by cprutean's avatar cprutean
Browse files

Update file fileIO.ipynb

parent 5a98f7d9
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:a3ba9097 tags:
# File Input and Output
In `Terminal Input and Output`, you learned how to read and write to the terminal window, but to deal with larger amounts of data we need to be able to read and write from and to files.
Here we will only consider text files, which are the same type of files
created by the editor and used to hold your `Python` source code.
## Opening files to Write
Before writing to a file you must first open it which is done with:
```python
fileout = open("mydata.dat","w")
```
- this will open a file called `mydata.dat` in your current directory with access code of `"w"` for *write* access.
- it will return a *filestream* in the variable `fileout`, which you will use to access the file. You do not need to concern youself with the details of what a *filestream* is at this point in your programming career; just use it.
- if the file `mydata.dat` already exists it will be *overwritten*, see more advanced use below to change this.
You can now write `str` to this file *line-by-line* with for example,
```python
x = 3.456e-6
fileout.write("The value of x is : " + str(x) + "\n")
```
which will form a single string and then write it to the opened file.
This has basic syntax as the `print()` that you used to write to the terminal, *except*
- the extra `"\n"` at the end which adds a *newline* character to the end of the written `str`
You can then simply repeat this `.write()` for as many other lines as you wish to write.
> #### Why the extra newline
The default `print()` command automatically adds the `"\n"`*behind-the-scenes* since it is *usually what you want*. This is sensible for output to a terminal, but for file output you want more control. The default `.write()` does only a write of the raw string that you supply, so you have to add the `"\n"` yourself.
> This is the common convention in all similar languages, for example `C` and `Java`.
### Key Point
So the easiest way to output to a file is:
1. open the file with `open(filename, "w")`
2. for each line convert to a string using `str()` and append a newline
character using ` + "\n"`
3. write the string to the *filestream* using `.write()`
This is sufficient to get you started and for many scientific `Python` programs that deal with small amounts of data this is the only strategy you will *ever need to use*.
> **Code Examples**
- Write strings to a file: [WriteFile](../CodeExamples/WriteFile.ipynb)
- Write values to a file from a for loop: [RangeLooptoFile](../CodeExamples/RangeLooptoFile.ipynb)
>- Write strings to a file: [WriteFile](../CodeExamples/WriteFile.ipynb)
>- Write values to a file from a for loop: [RangeLooptoFile](../CodeExamples/RangeLooptoFile.ipynb)
> Download and "play".
> Have a look around and "play".
## Opening files to Read
Reading from a file is basically the reverse of write, so firstly we
open the file, for example
```python
filein = open("mydata.dat","r")
```
1. will open a file called `mydata.dat` in your current directory for access key `"r"` (read access),
2. it will return the *filestream* in the variable `filein` which you will use to read from,
3. if the file `mydata.dat` does not exist you program *will* crash; you will learn how to trap this in later courses.
You would not normally *hard code* the filename into your program but
ask for it as shown in `Printing and reading to/from the Terminal`, so your code would be:
```python
filename = input("File to open : ")
filein = open(filename, "r")
```
which will ask you for the filename, and then open it.
Once you have the opened the file you can *read* from it. There are
*many* strategies for reading from files, but the simplest is using:
```python
lines = filein.readlines()
```
which will read in the *whole* file and return each line and as the
elements of a list of strings, so that
1. `lines[0]` is a string containing the first line of the file,
2. `lines[1]` is a string containing the second line of the file,
3. `len(lines)` is the number of lines successfully read.
How you now process this list of strings is the trickier bit, see next
section for details on this at [Parsing Input](ParsingInput.ipynb).
## Closing the file
When you have written or read the contents of a file you can close it
with
```python
filein.close()
```
where `filein` is the *filestream*. This will close the file and tidy up. When a program exits successfully or *crashes* all open files are automatically *closed* so this is not something you have worry about in short programs, but it is *good programming style* to close files after the read/write has been completed.
> **Code Examples**
- Read text file as strings: [ReadingandPrintingStrings](../CodeExamples/ReadingandPrintingStrings.ipynb)
>- Read text file as strings: [ReadingandPrintingStrings](../CodeExamples/ReadingandPrintingStrings.ipynb)
> Download and "play".
> Have a look around and "play".
### Other types of files
There many other types of files that hold *binary* data, for example
sound files, images (jpeg, png) , movies (mov, mp4), spreadsheets (xls,
xlsx) etc. These types cannot just processed as above and are much more
complex.
These are almost always read/written via library calls so the `Python` programmer does not need to know their internal structure; so for
example if you want to know how to read a `jpeg` image file, search (or Google) for the right module and the right call and use it, *“do not re-invent the wheel…”*.
For example you can read and display a `jpeg` image using Matlibplot with:
```python
import matplotlib.pyplot as plt
import matplotlib.image as img # Import image module
image = img.imread("myimage.jpeg") # Read the jpeg
plt.imshow(image) #render it in plot axis
plt.show() # display it
```
which does a default read and display without needing to know about the
internal complexities of `jpeg` images.
%% Cell type:code id: tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment