-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIO with basic file.py
More file actions
129 lines (55 loc) · 1.68 KB
/
IO with basic file.py
File metadata and controls
129 lines (55 loc) · 1.68 KB
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
#!/usr/bin/env python
# coding: utf-8
# In[9]:
# this %% magic is only available in jupyter notebook to write text file
# In[22]:
get_ipython().run_cell_magic('writefile', 'myfile.txt', 'hello this is a text file\nmy name is uzair')
# In[23]:
# opening file that created above
myfile = open('myfile.txt')
# In[24]:
myfile.read()
# In[25]:
pwd
# In[26]:
# after first time .read function returns empty string because when first time reads than cursor moves to end that is why
myfile.read()
# In[27]:
# solution -- seek(0) return the cursor to 0
myfile.seek(0)
myfile.read()
# In[28]:
myfile.seek(0)
myfile.readlines()
# In[ ]:
# open file with file lcoation
# full file path = 'C:\\User\\ ..' in windows
# full file path = 'User/yourname/ ..' in macOs
# myfile = open('full file path')
# In[30]:
# we should close after reada the file because its opened because it will return error 'this file is using somewhere else'
myfile.close()
# In[31]:
# to avoid previous error in shown in comment best practice to read the file is use special 'with statement'
with open('myfile.txt') as my_new_file:
contents = my_new_file.read()
# In[32]:
contents
# In[33]:
# we also use mode read or write
with open('myfile.txt',mode='r') as my_new_file:
contents = my_new_file.read()
# In[34]:
contents
# In[36]:
# r = read only
# w = write only
# a = append only
# r+ = is reading and writing
# w+ = is writng and reading overwrite existing file or creates a new file
with open('myfile.txt',mode='w') as my_new_file:
my_new_file.write('writng the file')
# In[37]:
with open('myfile.txt',mode='r') as my_new_file:
print(my_new_file.read())
# In[ ]: