Skip to content

Commit a813b1e

Browse files
authored
Add files via upload
1 parent ef67bf0 commit a813b1e

File tree

2 files changed

+431
-0
lines changed

2 files changed

+431
-0
lines changed
+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
2+
#======================= Operation on .txt file ============================
3+
4+
#import os
5+
6+
import os
7+
8+
os.getcwd() # Check current directory location
9+
10+
#Set the directory
11+
os.chdir('D:\\AA-SUNIL\\PROGRAMING AND DATABASE\\Programming\\Python\\My_Practice\\Program')
12+
13+
#Read .txt file
14+
newfile = open("C:\\Users\\HP\\Spyder\\Python_py.txt", "w+")
15+
16+
for i in range(1,10):
17+
print(i)
18+
newfile.write("\n Hello, Welcome to Python:") #Write in the file
19+
20+
newfile.close() #Close file
21+
22+
newfile = open("Python_Py.txt","r")
23+
24+
for i in range(1, 10):
25+
print(newfile.read())
26+
break
27+
28+
newfile.close()
29+
30+
31+
# File methods
32+
33+
os.rename("Python_Py.txt","Python2.txt")
34+
35+
os.remove("Python2.txt")
36+
37+
os.listdir(os.getcwd())
38+
39+
os.mkdir('new')
40+
41+
os.chdir('C:\\Users\\SUNEEL PATEL\\Desktop\\py\\D3')
42+
43+
44+
#2.===================== Operation on .csv files =============================
45+
46+
fp = open("USArrests.csv", "r")
47+
for line in fp:
48+
print(line)
49+
50+
print(fp.read())
51+
fp.close()
52+
53+
54+
import codecs
55+
56+
fp = codecs.open(os.path.join('C:\\Users\\SUNEEL PATEL\\Desktop\\py\\D2','worldcities.csv'), 'r')
57+
cnt = 0
58+
for line in fp:
59+
print (cnt)
60+
if cnt <= 10:
61+
print(line)
62+
else:
63+
break
64+
cnt += 1
65+
fp.close()
66+
67+
68+
os.rename('C:\\Users\\NAEEN REDDY\\Desktop\\py\\D2\\worldcities.csv',
69+
os.path.join('C:\\Users\\SUNEEL PATEL\\Desktop\\','County.csv'))
70+
71+
72+
73+
wp = open(os.path.join('C:\\Users\\SUNEEL PATEL\\Desktop\\py\\D2\\', 'counry.DAT'), 'w')
74+
75+
rp = open(os.path.join('C:\\Users\\SUNEEL PATEL\\Desktop\\py\\D2','worldcities.csv'), 'r')
76+
cnt = 0
77+
for line in rp:
78+
if cnt <= 10:
79+
wp.write(line)
80+
wp.write('\n')
81+
else:
82+
break
83+
cnt += 1
84+
rp.close()
85+
wp.close()
86+
87+
88+
for line in reversed(list(open(os.path.join('C:\\Users\\NAEEN REDDY\\Desktop\\py\\D2','worldcities.csv'), 'r'))):
89+
print(line.rstrip())
90+
91+
92+
93+
94+
d1 = {'fname': 'Steve', 'lname': 'Jobs'}
95+
d2 = {'fname': 'Steve', 'lname': 'Jobs', 'age': 21}
96+
97+
import json
98+
wp = open(os.path.join('C:\\Users\\SUNEEL PATEL\\Desktop\\py\\D2', 'test_new.json'), 'w+')
99+
for x in range(3):
100+
if x == 0:
101+
wp.write(json.dumps(d1))
102+
elif x == 1:
103+
wp.write(json.dumps(d2))
104+
else:
105+
wp.write(json.dumps({'age':24, 'salary':10000}))
106+
wp.write('\n')
107+
wp.close()
108+
109+
110+
111+
#import json
112+
wp = open(os.path.join('C:\\Users\\SUNEEL PATEL\\Desktop\\py\\D2', 'test_new.json'), 'w+')
113+
for x in range(5):
114+
if x == 0:
115+
wp.write(json.dumps(d1))
116+
elif x == 1:
117+
wp.write(json.dumps(d2))
118+
else:
119+
wp.write(json.dumps({'age':24, 'salary':10000}))
120+
wp.write('\n')
121+
wp.close()
122+
123+
124+
125+
#import json
126+
rp = open(os.path.join('C:\\Users\\SUNEEL PATEL\\Desktop\\py\\D2', 'test_new.json'), 'r')
127+
# content = json.loads(rp.read())
128+
for line in rp:
129+
#print(line)
130+
rec = json.loads(line)
131+
#print(rec, type(rec))
132+
print(rec.get('salary', 'Not found'))
133+
134+
rp.close()
135+
136+
137+
138+
# d1['key'] and d1[key]
139+
path = os.path.join('C:\\Users\\SUNEEL PATEL\\Desktop\\py\\D2', 'test_new.json')
140+
print(path)
141+
142+
143+
os.path.exists(os.path.join('Misc/old', 'test.json'))
144+
145+
146+
os.path.exists(os.path.join('C:\\Users\\SUNEEL PATEL\\Desktop\\py\\D2', 'test_new.json'))
147+
148+
#==============================================================================
149+
150+
"""
151+
Suneel Patel - Data Scientist for Nation, Society and Humanity
152+
153+
Learn & Teach - Online Training Platform
154+
155+
Email: [email protected] | M.No: +91-7771007070
156+
Follow on LinkedIn : https://www.linkedin.com/in/suneelpatel/
157+
Twitter: https://twitter.com/sunilpatel18
158+
159+
"""

0 commit comments

Comments
 (0)