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