|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +# # File Handling using Python |
| 5 | + |
| 6 | +# In[1]: |
| 7 | + |
| 8 | + |
| 9 | +# open(filename, mode) |
| 10 | + |
| 11 | +# r - reading |
| 12 | +# w - writing |
| 13 | +# a - appending |
| 14 | + |
| 15 | + |
| 16 | +# In[7]: |
| 17 | + |
| 18 | + |
| 19 | +file = open('social.txt', 'r') |
| 20 | +data = file.read() |
| 21 | + |
| 22 | +# if for |
| 23 | + |
| 24 | +file.close() |
| 25 | + |
| 26 | +print(file.closed) |
| 27 | + |
| 28 | + |
| 29 | +# In[11]: |
| 30 | + |
| 31 | + |
| 32 | +# context manager |
| 33 | + |
| 34 | +with open('social.txt', 'r') as file: |
| 35 | + data = file.read() |
| 36 | + |
| 37 | +print(file.closed) |
| 38 | + |
| 39 | + |
| 40 | +# In[15]: |
| 41 | + |
| 42 | + |
| 43 | +with open('something.txt', 'a') as file: |
| 44 | + file.write('MMCOE - yethe bahutanche hith!') |
| 45 | + |
| 46 | + |
| 47 | +# In[18]: |
| 48 | + |
| 49 | + |
| 50 | +with open('social.txt', 'r') as fd: |
| 51 | + data = fd.read(6) # how many bytes or characters you have to read |
| 52 | + |
| 53 | +print(data) |
| 54 | + |
| 55 | + |
| 56 | +# In[21]: |
| 57 | + |
| 58 | + |
| 59 | +import os |
| 60 | + |
| 61 | +print(os.getcwd()) |
| 62 | + |
| 63 | + |
| 64 | +# In[22]: |
| 65 | + |
| 66 | + |
| 67 | +print('Original Directory:', os.getcwd()) |
| 68 | +os.chdir('/home/omkarpathak/Documents/Notebooks') |
| 69 | +print('Current Directory:', os.getcwd()) |
| 70 | + |
| 71 | + |
| 72 | +# In[24]: |
| 73 | + |
| 74 | + |
| 75 | +print(os.listdir()) |
| 76 | + |
| 77 | + |
| 78 | +# In[30]: |
| 79 | + |
| 80 | + |
| 81 | +files = [] |
| 82 | + |
| 83 | +files = os.listdir() |
| 84 | + |
| 85 | +# os.path.isfile(filename) # return True is it is a file, else it returns False |
| 86 | + |
| 87 | +for file in files: |
| 88 | + if os.path.isfile(os.path.abspath(file)): |
| 89 | + print('File:', file) |
| 90 | + |
| 91 | +for file in files: |
| 92 | + if os.path.isdir(os.path.abspath(file)): |
| 93 | + print('Dir:', file) |
| 94 | + |
| 95 | + |
| 96 | +# In[68]: |
| 97 | + |
| 98 | + |
| 99 | +os.chdir('/home/omkarpathak/Documents/PythonLecture/Naruto/Directory0') |
| 100 | + |
| 101 | +for i in range(10): |
| 102 | + os.mkdir('Directory' + str(i) + str(i)) # creating a folder |
| 103 | + os.chdir('Directory' + str(i) + str(i)) # Directory0 -> Directory1 |
| 104 | + with open('something.txt', 'w') as file: |
| 105 | + file.write('Text') |
| 106 | + os.chdir('/home/omkarpathak/Documents/PythonLecture/Naruto/Directory0') |
| 107 | + |
| 108 | + |
| 109 | +# In[47]: |
| 110 | + |
| 111 | + |
| 112 | +os.chdir('/home/omkarpathak/Documents/PythonLecture/') |
| 113 | + |
| 114 | + |
| 115 | +# In[44]: |
| 116 | + |
| 117 | + |
| 118 | +mylist = ['omkar', 1, 3, 4.0] |
| 119 | +print(mylist) |
| 120 | + |
| 121 | + |
| 122 | +# In[53]: |
| 123 | + |
| 124 | + |
| 125 | +word_to_check = 'hilarious' |
| 126 | + |
| 127 | +with open('social.txt', 'r') as file: |
| 128 | + data = file.read() |
| 129 | + |
| 130 | +for line in data.split('\n'): |
| 131 | + if word_to_check in line: |
| 132 | + print('True') |
| 133 | + print(line) |
| 134 | + |
| 135 | +print(len(data.split('\n'))) |
| 136 | + |
| 137 | + |
| 138 | +# In[72]: |
| 139 | + |
| 140 | + |
| 141 | +os.chdir('/home/omkarpathak/Documents/PythonLecture') |
| 142 | +with open('social.txt', 'r') as file: |
| 143 | + data = file.read() |
| 144 | + |
| 145 | +char = 'M' |
| 146 | +# print(data.lower()) |
| 147 | +print(data.lower().count(char.lower())) |
| 148 | + |
| 149 | + |
| 150 | +# In[60]: |
| 151 | + |
| 152 | + |
| 153 | +with open('social.txt', 'r') as file: |
| 154 | + data = file.read() |
| 155 | + |
| 156 | +char = input('Enter a character of your choice:') |
| 157 | + |
| 158 | +print(data.lower().count(char)) |
| 159 | + |
| 160 | + |
| 161 | +# In[76]: |
| 162 | + |
| 163 | + |
| 164 | +string = 'My namename is Omkar' |
| 165 | +print(string.count('name')) # fuzzy search |
| 166 | + |
| 167 | + |
| 168 | +# In[ ]: |
| 169 | + |
| 170 | + |
| 171 | +import numpy |
| 172 | +import scipy |
| 173 | +import matplotlib |
| 174 | + |
0 commit comments