Created
July 5, 2020 13:50
-
-
Save upsharma8/4f2a8f7308a64abe6c2701b501439457 to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#Opening a File\n", | |
"f1=open(\"file.txt\");" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'file.txt'" | |
] | |
}, | |
"execution_count": 26, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"f1.name" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'r'" | |
] | |
}, | |
"execution_count": 27, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"f1.mode" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 82, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"f1.close();" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 59, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#Reading File" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"This is line 1 \n", | |
"THis is line 2 \n", | |
"THis is line 3\n", | |
"True\n", | |
"This is line 1 \n", | |
"THis is line 2 \n", | |
"THis is line 3\n" | |
] | |
} | |
], | |
"source": [ | |
"with open(\"file.txt\") as f:\n", | |
" file_read=f.read();\n", | |
" print(file_read);\n", | |
"print(f.closed);\n", | |
"print(file_read);" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['This is line 1 \\n', 'THis is line 2 \\n', 'THis is line 3\\n']\n" | |
] | |
} | |
], | |
"source": [ | |
"with open(\"file.txt\",'r') as f:\n", | |
" file_read=f.readlines();\n", | |
" print(file_read);\n", | |
" \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"This is line 1 \n", | |
"\n", | |
"THis is line 2 \n", | |
"\n", | |
"THis is line 3\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"with open(\"file.txt\",'r') as f:\n", | |
" file_read=f.readline();\n", | |
" print(file_read);\n", | |
" file_read=f.readline();\n", | |
" print(file_read);\n", | |
" file_read=f.readline();\n", | |
" print(file_read);\n", | |
" \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"This is line 1 \n", | |
"\n", | |
"THis is line 2 \n", | |
"\n", | |
"THis is line 3\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"with open(\"file.txt\",'r') as f:\n", | |
" for i in f:\n", | |
" print(i);" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 56, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"This\n", | |
" is li\n", | |
"ne 1 \n", | |
"\n", | |
"THis is line 2 \n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"with open(\"file.txt\",'r') as fi:\n", | |
" files=fi.readline(4);\n", | |
" print(files);\n", | |
" files=fi.readline(6);\n", | |
" print(files);\n", | |
" files=fi.readline(9);\n", | |
" print(files);\n", | |
" files=fi.readline(16);\n", | |
" print(files);" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#Writing Files \n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"f=open(\"files.txt\",'w');\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 76, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"with open(\"files.txt\",\"w\") as f:\n", | |
" f.write(\"hello world\\n\");\n", | |
" f.write(\"Line 2\\n\");\n", | |
" f.write(\"Line 3\\n\");\n", | |
" f.write(\"Line 4\");\n", | |
" \n", | |
" \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 77, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"Lines=[\"Line 1\\n\",\"Line 2\\n\"];\n", | |
"with open(\"files2.txt\",\"w\") as f:\n", | |
" for i in Lines:\n", | |
" f.write(i);" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 78, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#Appending in a file\n", | |
"with open(\"files2.txt\",\"a\") as f:\n", | |
" f.write(\"This is end line\");" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 79, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#Copy from one file to new file" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 81, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"with open(\"file.txt\",\"r\") as f:\n", | |
" with open(\"files3.txt\",\"w\") as fi:\n", | |
" for i in f:\n", | |
" fi.write(i);" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python", | |
"language": "python", | |
"name": "conda-env-python-py" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.10" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment