├── .idea ├── .gitignore ├── idle.iml ├── inspectionProfiles │ └── profiles_settings.xml ├── misc.xml └── modules.xml ├── Files In Python.ipynb ├── README.md ├── append.py ├── demo.txt ├── demo1.txt ├── h.html ├── hello.txt ├── read.py └── write.py /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /workspace.xml -------------------------------------------------------------------------------- /.idea/idle.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Files In Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Why we need a file?\n", 8 | "Answer- Most of data and variable that we use are temporary in nature.If you do count something then you have to use varibale but that variable is temporary.Ofcourse the moment you close the application you will loose all the data.but,i want even i closed my application my data store somewhere .And That's why you need to find permanent storage.\n", 9 | "\n", 10 | "One of way is that you can use relational database (MySQL,Oracle) but these provide you table structure which is very complex stuff.\n", 11 | "\n", 12 | "If you want to store data for a longer peroid of time as well as simple way best way to go for Files.\n", 13 | "\n", 14 | "simply you can say Files are named locations on disk to store related information. They are used to permanently store data in a non-volatile memory (e.g. hard disk).\n", 15 | "Since Random Access Memory (RAM) is volatile (which loses its data when the computer is turned off), we use files for future use of the data by permanently storing them.\n", 16 | "\n", 17 | "in Python, a file operation takes place in the following order:\n", 18 | "\n", 19 | "1.Open a file\n", 20 | "2.Read or write (perform operation)\n", 21 | "3.Close the file\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "# There are two types of files that can be handled in Python, normal text files and binary files (written in binary language, 0s and 1s).\n", 29 | "\n", 30 | "# Text files: \n", 31 | "In this type of file, Each line of text is terminated with a special character called EOL (End of Line), which is the new line character (‘\\n’) in Python by default.\n", 32 | "# Binary files: \n", 33 | "In this type of file, there is no terminator for a line and the data is stored after converting it into machine-understandable binary language." 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "# Opening Files in Python\n", 41 | "Python has a built-in open() function to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingl\n", 42 | "Syntax:\n", 43 | " file_name=open(\"filename.txt\",accessmode)#it takes two arguments.\n", 44 | " " 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "# There are three access mode present in file -\n", 52 | "\n", 53 | "# 1.(r)-readmode:\n", 54 | " ->only for reading the content.\n", 55 | " ->file pointer points at the begining of the file.\n", 56 | " ->File should exits before opining in read mode.\n", 57 | " Syntax-\n", 58 | " file_name=open(\"filename.txt\",\"r\")\n", 59 | " \n", 60 | "# 2.(w)-writemode: \n", 61 | " ->only for reading the content.\n", 62 | " ->file pointer points at the begining of the file.\n", 63 | " 1.If the file exits the file will open and pointer point at the begining and the data will be overwritten.\n", 64 | " 2.If the file doesn't exits then new file will be created with the given file name.\n", 65 | " Syntax-\n", 66 | " file_name=open(\"filename.txt\",\"w\") \n", 67 | " \n", 68 | "# 3.(a)-appendmode:\n", 69 | " ->to append and write the data.\n", 70 | " ->If the file exits then file pointer points at the end and the content in the file.\n", 71 | " ->If the file doesn't exits the New file will be created and file pointer points at the begining.\n", 72 | "#To know more click this link-https://www.youtube.com/watch?v=vuyb7CxZgbU&list=PLLOxZwkBK52DmuHRO3UNpqAzDF57FtIxk&index=35 " 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 7, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "name": "stdout", 82 | "output_type": "stream", 83 | "text": [ 84 | "hello welcome\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "#https://www.youtube.com/watch?v=94ZqI4oQNc0&t=600s\n", 90 | "f1=open(\"hello.txt\",\"w\")\n", 91 | "f1.write(\"hello welcome\")\n", 92 | "f1.close()\n", 93 | "f2=open(\"hello.txt\",'r')\n", 94 | "#print(f2.read(2))\n", 95 | "#f1.close()\n", 96 | "print(f2.read())" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 6, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "data": { 106 | "text/plain": [ 107 | "'C:\\\\Users\\\\Kushal'" 108 | ] 109 | }, 110 | "execution_count": 6, 111 | "metadata": {}, 112 | "output_type": "execute_result" 113 | } 114 | ], 115 | "source": [ 116 | "#To know yoyr current directory\n", 117 | "import os\n", 118 | "os.getcwd()" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "help(open)" 128 | ] 129 | } 130 | ], 131 | "metadata": { 132 | "kernelspec": { 133 | "display_name": "Python 3", 134 | "language": "python", 135 | "name": "python3" 136 | }, 137 | "language_info": { 138 | "codemirror_mode": { 139 | "name": "ipython", 140 | "version": 3 141 | }, 142 | "file_extension": ".py", 143 | "mimetype": "text/x-python", 144 | "name": "python", 145 | "nbconvert_exporter": "python", 146 | "pygments_lexer": "ipython3", 147 | "version": "3.7.4" 148 | } 149 | }, 150 | "nbformat": 4, 151 | "nbformat_minor": 2 152 | } 153 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PythonFileHandling 2 | -------------------------------------------------------------------------------- /append.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Note** 3 | if you append something in a existance file and your existance file keeps 4 | some data the after append something it will not remove your privious data 5 | like in write().It will just append your data. 6 | file. 7 | ''' 8 | f1=open("demo1.txt",'a') 9 | f2=f1.write("Hello ML intern") 10 | f1.close() 11 | f2=open("demo1.txt",'r') 12 | print(f2.read()) -------------------------------------------------------------------------------- /demo.txt: -------------------------------------------------------------------------------- 1 | Wisdom Quotes: 2 | You can do anything, but not everything. 3 | The richest man is not he who has the most, but he who needs the least. 4 | You miss 100 percent of the shots you never take. 5 | Courage is not the absence of fear, but rather the judgement that something else is more important than fear. 6 | When hungry, eat your rice; when tired, close your eyes. Fools may laugh at me, but wise men will know what I mean. 7 | To the man who only has a hammer, everything he encounters begins to look like a nail. 8 | -------------------------------------------------------------------------------- /demo1.txt: -------------------------------------------------------------------------------- 1 | I love to do python programming 2 | I love to do cpp as well 3 | Hello ML intern -------------------------------------------------------------------------------- /h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Title 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /hello.txt: -------------------------------------------------------------------------------- 1 | hello welcome -------------------------------------------------------------------------------- /read.py: -------------------------------------------------------------------------------- 1 | #Here what we can do just read the file 2 | 3 | #First we have to open the file for reading purpose. 4 | 5 | f1=open("demo.txt","r")#Here i open the demo.txt file. 6 | 7 | ''' 8 | read() : Returns the read bytes in form of a string. Reads n bytes, 9 | if no n specified, reads the entire file. 10 | Syntax: File_object.read([n]) 11 | ''' 12 | print('Here is the output of read()=\n',f1.read())#here i read all the documents from the demo.txt file and print it. 13 | 14 | ''' 15 | readline() : Reads a line of the file and returns in form of a string. 16 | For specified n, reads at most n bytes. 17 | However, does not reads more than one line, 18 | even if n exceeds the length of the line. 19 | Syntax : File_object.readline([n]) 20 | ''' 21 | f2=open("demo.txt",'r') 22 | print('Here is the output of readline()=\n',f2.readline())#it will display the single line 23 | 24 | 25 | ''' 26 | readlines() : Reads all the lines and return them as each line a string element in a list. 27 | syntax :File_object.readlines() 28 | ''' 29 | f3=open("demo.txt",'r') 30 | print('Here is the output of readlines()=\n',f3.readlines()) 31 | -------------------------------------------------------------------------------- /write.py: -------------------------------------------------------------------------------- 1 | '''Note** 2 | if you write something in a existance file and your existance file keeps 3 | some data the after writing something it will be remove your privious data and meanwhile it will append new data from that 4 | file. 5 | 6 | close() is must in write() because if you write something and you want to make 7 | changes and you don't use close() as a result you're unable to do changes. 8 | ''' 9 | 10 | 11 | 12 | #Here we see how to write a file 13 | ''' 14 | write() : Inserts the string str1 in a single line in the text file 15 | Syntax:File_object.write(str1) 16 | ''' 17 | f1=open("demo1.txt","w") 18 | f2=f1.write("Hello data science intern") 19 | f1.close() 20 | f2=open("demo1.txt",'r') 21 | print(f2.read()) 22 | 23 | ''' 24 | writelines() : For a list of string elements, each string is inserted in the text file. Used to insert multiple strings at a single time. 25 | File_object.writelines(L) for L = [str1, str2, str3] 26 | ''' 27 | 28 | f1=open("demo1.txt","w") 29 | l=["I love to do python programming\n","I love to do cpp as well\n"] 30 | f2=f1.writelines(l) 31 | f1.close() 32 | f2=open("demo1.txt",'r') 33 | print(f2.read()) --------------------------------------------------------------------------------