Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Files are semi done, just pushing some so that if my computer blows u…
…p it's not just here anymore
- Loading branch information
Barnsa
committed
Oct 13, 2020
1 parent
6468570
commit d4fb11d
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,48 @@ | ||
# Reading and Writing Files | ||
<!-- {{todo("Write this section")}} --> | ||
|
||
{{todo("Write this section")}} | ||
## Finding files on your system | ||
Before we go into detail about how to interact with a file it's worth noting the ways in which you can find files on your system. Python has a couple of ways to do it but you must also understand the difference between absolute paths and relative paths in programming. | ||
|
||
### Absolute paths | ||
Absolute paths are the way in which your system stores files for you to find later. It is the full path directory in which the file is located, although it is different between Linux and Windows systems. In a Linux system, an absolute path starts with '/root/...' and in a Windows system 'C:\\...'. | ||
|
||
Absolute paths are very long sometimes and are also different from system to system. Regardless of the host operating system, it's smart to assume that someone can move or rename files on their system, that their username directories are different, or that they have chosen a different location to install the malware you have so carefully crafted. Because of this, absolute paths have limited uses in programming directly, it's better to force the program to work out where it's own absolute path is than type it directly in. Consider this code: | ||
|
||
{{ code_from_file("files/example-1.py") }} | ||
|
||
??? Example | ||
For this example, all we are really doing is printing out the paths that have been calculated by different methods. It's worth noting the `__file__` function, also known as a 'dunder' because it's surrounded in double underscores, is a way to find the name of the file that you are currently running in python. But what happens if you need to access the file next to the one that is running?? That's where relative paths come in. These methods also only find the path and not the name of the actual file, the filename is 'example-1.py' and the program thinks it's called 'main.py', try it for yourself!! | ||
|
||
### Relative paths | ||
Because absolute paths can't always easily find a file relative to the one running the code, relative paths were also created. To find a relative path you need it to be relative to something. Take a look at this example: | ||
|
||
{{ code_from_file("files/example-2.py") }} | ||
|
||
|
||
If you guys want more information or another opinion on working with paths in Mac, Windows, and Linux you can find one through Medium [here](https://medium.com/@ageitgey/python-3-quick-tip-the-easy-way-to-deal-with-file-paths-on-windows-mac-and-linux-11a072b58d5f#:~:text=To%20use%20it%2C%20you%20just,for%20the%20current%20operating%20system.). | ||
|
||
## Opening files | ||
Opening files for reading and writing is something every programmer must master at some point. In a lot of programming languages you can encounter memory faults and broken programs just by failing to open a file. If the clean up after a file being opened, such as clearing the extraneous data from memory, then you can quickly run into buffer issues and data overwriting itself if you're not careful. | ||
|
||
Python on the other hand does a lot of this hard work for us. It has a lot of built-in cleanup functions and a lot of memory protections already there so that there is less chance of these kinds of errors and issues. The only thing is you have to make sure you use them!! | ||
|
||
Take a look at this code and see if you can work out what's going on: | ||
|
||
{{ code_from_file("files/example-6.py") }} | ||
|
||
!!! Example | ||
In this example we can see two ways in which you can open files and interact with files in python. The first way is the older way and doesn't include any inherent error catching, and if an exception is thrown during the file opening it doesn't close the file and can leave it in a vulnerable state. The other way using the "with" statement, ensures that the file will close regardless of what happens to it. We even go so far as to prove it with a test. | ||
|
||
When writing your own file opening and closing then make sure that you use the with statements in your code. It's also shorter than the other ways when you include the error checking try/except blocks too, so good for the lazier/efficient programmer. | ||
|
||
If you would like more examples you can search for them pretty easily on our old pal google or find some [here](https://www.tutorialspoint.com/python3/python_files_io.htm) | ||
|
||
## Reading, writing, appending. | ||
You may have also noticed that the open statements have a flag that is associated with them. This flag is generally used to open the file in a specific way so as to allow reading, writing, appending and such to the file. Here is a complete table of the flags: | ||
|
||
|||| | ||
|
||
|
||
!!! Note | ||
If you want another article to look at file includes, paths and working with files in general, you can find realpython's one [here](https://realpython.com/read-write-files-python/#working-with-two-files-at-the-same-time) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env python3 | ||
# small program to show how absolute values can be found in python | ||
|
||
import os | ||
from pathlib import Path | ||
|
||
# the os way is considered the older way | ||
# for anything less than python 3.4 | ||
# the __file__ will use this files name | ||
print(f"os.path = {os.path.abspath(__file__)}") | ||
|
||
|
||
# The modern way is to use pathlib | ||
# this is only good for versions of | ||
# python above 3.4 | ||
# the __file__ will use this files name | ||
print(f"pathlib = {Path(__file__)}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env python3 | ||
# A small program to illustrate relative paths in python | ||
|
||
# library set-up | ||
import os | ||
from pathlib import Path | ||
|
||
|
||
# The os version again is the old way. | ||
# it's still a perfectly valid way to find | ||
# files and relative paths in your operating | ||
# system though. | ||
print(f"os.path = {os.path.join(os.path.dirname(os.path.abspath(__file__)), 'file.txt')}") | ||
|
||
|
||
# the pathlib way is a the better and more | ||
# modern way of doing paths in python. You will | ||
# need python 3.4 or above for this to work | ||
# however. | ||
p = Path(__file__) | ||
file_extension = '\\file.txt' | ||
print(f"pathlib = {str(p.parent) + file_extension}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
This | ||
is | ||
a | ||
file | ||
containing | ||
not | ||
an | ||
awful | ||
lot | ||
I'm | ||
afraid | ||
... | ||
but | ||
at | ||
least | ||
there | ||
is | ||
some | ||
data | ||
here! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters