Skip to content

Commit

Permalink
fixed possible issues with files
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierian-Data committed Feb 14, 2018
1 parent c16e0f9 commit 240c0b0
Show file tree
Hide file tree
Showing 4 changed files with 498 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = set()"
Expand All @@ -25,7 +27,9 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# We add to sets with the add() method\n",
Expand Down Expand Up @@ -65,7 +69,9 @@
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Add a different element\n",
Expand Down Expand Up @@ -96,7 +102,9 @@
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Try to add the same element\n",
Expand Down Expand Up @@ -134,7 +142,9 @@
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Create a list with repeats\n",
Expand Down Expand Up @@ -174,7 +184,9 @@
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Set object to be a boolean\n",
Expand Down Expand Up @@ -240,7 +252,9 @@
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# None placeholder\n",
Expand Down Expand Up @@ -289,7 +303,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
"version": "3.6.1"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"Python has a built-in open function that allows us to open and play with basic file types. First we will need a file though. We're going to use some IPython magic to create a text file!\n",
"\n",
"## IPython Writing a File \n",
"#### This function is specific to jupyter notebooks!"
"#### This function is specific to jupyter notebooks! Alternatively, quickly create a simple .txt file with sublime text editor."
]
},
{
Expand All @@ -38,13 +38,80 @@
"source": [
"## Python Opening a file\n",
"\n",
"We can open a file with the open() function. The open function also takes in arguments (also called parameters). Lets see how this is used:"
"Let's being by opening the file test.txt that is located in the same directory as this notebook. For now we will work with files located in the same directory as the notebook or .py script you are using.\n",
"\n",
"It is very easy to get an error on this step:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: 'whoops.txt'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-dafe28ee473f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmyfile\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'whoops.txt'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'whoops.txt'"
]
}
],
"source": [
"myfile = open('whoops.txt')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To avoid this error,make sure your .txt file is saved in the same location as your notebook, to check your notebook location, use **pwd**:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'C:\\\\Users\\\\Marcial\\\\Pierian-Data-Courses\\\\Complete-Python-3-Bootcamp\\\\00-Python Object and Data Structure Basics'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pwd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Alternatively, to grab files from any location on your computer, simply pass in the entire file path. **\n",
"\n",
"For Windows you need to use double \\ so python doesn't treat the second \\ as an escape character, a file path is in the form:\n",
"\n",
" myfile = open(\"C:\\\\Users\\\\YourUserName\\\\Home\\\\Folder\\\\myfile.txt\")\n",
"\n",
"For MacOS and Linux you use slashes in the opposite direction:\n",
"\n",
" myfile = open(\"/Users/YouUserName/Folder/myfile.txt\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Open the text.txt we made earlier\n",
Expand Down Expand Up @@ -181,7 +248,9 @@
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"my_file.close()"
Expand All @@ -199,7 +268,9 @@
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Add a second argument to the function, 'w' which stands for write.\n",
Expand Down Expand Up @@ -262,7 +333,9 @@
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"my_file.close() # always do this when you're done with a file"
Expand Down Expand Up @@ -321,7 +394,9 @@
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"my_file.close()"
Expand Down Expand Up @@ -473,7 +548,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
"version": "3.6.1"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 240c0b0

Please sign in to comment.