- Notifications
You must be signed in to change notification settings - Fork 54
File io #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good overall. Although it does seem a little silly to open a context manager to call np.save
instead of just calling save
with a path.
"\n", | ||
"**Takeaway**: \n", | ||
"\n", | ||
"You should strive to utilize `path.Path` whenever you are working with file system paths in your code. To reiterate - this will ensure that your code is portable across operating systems, it will help make your path handling easy to read, plus this class's methods provides a massive amount of functionality for you to leverage at your convenience.\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strive to utilize pathlib.Path
(not path.Path
)
"metadata": {}, | ||
"source": [ | ||
"## Opening Files\n", | ||
"It is recommended that you refer to the [official python tutorial](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files) for a simple rundown of file reading and writing\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
official Python tutorial -- capitalization
Also add a period at the end.
"\n", | ||
"Whenever you instruct your code to open a file for reading or writing, you must take care that the file ultimately is closed so that its data is not vulnerable to being modified. Python provides the `open` context manager, which is designed to ensure that a file will be closed even in the event that our code raises an error.\n", | ||
"\n", | ||
"The following code opens the file \"file1.txt\", which is and writes to the file:\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"which is and writes to" <-- ??
"# demonstrating the use of the `open` context manager\n", | ||
"\n", | ||
"path_to_file = Path(\".\") / \"file1.txt\"\n", | ||
"with open(path_to_file, mode=\"w\") as f:\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be worth mentioning that you can also just call .open()
on Path
objects
"path_to_file = Path(\".\") / \"file1.txt\"\n", | ||
"with open(path_to_file, mode=\"w\") as f:\n", | ||
" # The indented space enters the \"context\" of the open file.\n", | ||
" # Leaving the indented space exists the context of the opened file, forcing\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exists -> exits
"\n", | ||
"By default, these modes will read and write text utilizing the unicode (utf-8) decoding/encoding specification. That is, when you read data from your file system with `mode='r'` Python will automatically *decode* that binary data that was stored on your machine according to utf-8, which converts the binary data to written text stored as a string. Similarly, writing a string to a file in modes 'w', 'a', 'x', or '+' will presume that the string should be encoded into a binary representation (which is necessary for it to be stored as a file) according to the utf-8 encoding scheme.\n", | ||
"\n", | ||
"You can instead force Python to read and write strictly in terms of binary data by adding a `'b'` to these modes: `'rb'`, `'wb'`, `'ab'`, `'xb'`, `'+b'`. It is important to be aware of this binary mode. For example, if you are saving a numpy-array, you should open a file in the 'wb' or 'xb' modes so that it expects binary data to be written to it; obviously we are not saving text when we are saving a numpy array of numbers.\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NumPy -- caps
"This is the third line.\n", | ||
"```\n", | ||
"\n", | ||
"The following is summarizes some of the methods available to this file object:\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"is summarizes" -> summarizes
Blegh