mirror of
https://github.com/apachecn/ailearning.git
synced 2026-04-24 02:23:45 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%writefile test.txt
|
||||
this is a test file.
|
||||
hello world!
|
||||
@@ -13,7 +13,7 @@ today is a good day.
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Writing test.txt
|
||||
|
||||
```
|
||||
@@ -24,14 +24,14 @@ Writing test.txt
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
f = open('test.txt')
|
||||
|
||||
```
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
f = file('test.txt')
|
||||
|
||||
```
|
||||
@@ -44,13 +44,13 @@ f = file('test.txt')
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
text = f.read()
|
||||
print text
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
this is a test file.
|
||||
hello world!
|
||||
python is good!
|
||||
@@ -62,14 +62,14 @@ today is a good day.
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
f = open('test.txt')
|
||||
lines = f.readlines()
|
||||
print lines
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
['this is a test file.\n', 'hello world!\n', 'python is good!\n', 'today is a good day.']
|
||||
|
||||
```
|
||||
@@ -78,7 +78,7 @@ print lines
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
f.close()
|
||||
|
||||
```
|
||||
@@ -87,7 +87,7 @@ f.close()
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
f = open('test.txt')
|
||||
for line in f:
|
||||
print line
|
||||
@@ -95,7 +95,7 @@ f.close()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
this is a test file.
|
||||
|
||||
hello world!
|
||||
@@ -110,7 +110,7 @@ today is a good day.
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
import os
|
||||
os.remove('test.txt')
|
||||
|
||||
@@ -122,7 +122,7 @@ os.remove('test.txt')
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
f = open('myfile.txt', 'w')
|
||||
f.write('hello world!')
|
||||
f.close()
|
||||
@@ -133,12 +133,12 @@ f.close()
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
print open('myfile.txt').read()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
hello world!
|
||||
|
||||
```
|
||||
@@ -147,7 +147,7 @@ hello world!
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
f = open('myfile.txt', 'w')
|
||||
f.write('another hello world!')
|
||||
f.close()
|
||||
@@ -155,7 +155,7 @@ print open('myfile.txt').read()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
another hello world!
|
||||
|
||||
```
|
||||
@@ -164,7 +164,7 @@ another hello world!
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
f = open('myfile.txt', 'a')
|
||||
f.write('... and more')
|
||||
f.close()
|
||||
@@ -172,7 +172,7 @@ print open('myfile.txt').read()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
another hello world!... and more
|
||||
|
||||
```
|
||||
@@ -183,7 +183,7 @@ another hello world!... and more
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
f = open('myfile.txt', 'w+')
|
||||
f.write('hello world!')
|
||||
f.seek(6)
|
||||
@@ -192,7 +192,7 @@ f.close()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
world!
|
||||
|
||||
```
|
||||
@@ -201,7 +201,7 @@ world!
|
||||
|
||||
In [14]:
|
||||
|
||||
```
|
||||
```py
|
||||
import os
|
||||
os.remove('myfile.txt')
|
||||
|
||||
@@ -213,7 +213,7 @@ os.remove('myfile.txt')
|
||||
|
||||
In [15]:
|
||||
|
||||
```
|
||||
```py
|
||||
import os
|
||||
f = open('binary.bin', 'wb')
|
||||
f.write(os.urandom(16))
|
||||
@@ -225,14 +225,14 @@ f.close()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
'\x86H\x93\xe1\xd8\xef\xc0\xaa(\x17\xa9\xc9\xa51\xf1\x98'
|
||||
|
||||
```
|
||||
|
||||
In [16]:
|
||||
|
||||
```
|
||||
```py
|
||||
import os
|
||||
os.remove('binary.bin')
|
||||
|
||||
@@ -258,7 +258,7 @@ os.remove('binary.bin')
|
||||
|
||||
In [17]:
|
||||
|
||||
```
|
||||
```py
|
||||
f = open('newfile.txt','w')
|
||||
f.write('hello world')
|
||||
g = open('newfile.txt', 'r')
|
||||
@@ -266,7 +266,7 @@ print repr(g.read())
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
''
|
||||
|
||||
```
|
||||
@@ -277,7 +277,7 @@ print repr(g.read())
|
||||
|
||||
In [18]:
|
||||
|
||||
```
|
||||
```py
|
||||
f = open('newfile.txt','w')
|
||||
for i in range(3000):
|
||||
f.write('hello world: ' + str(i) + '\n')
|
||||
@@ -289,7 +289,7 @@ g.close()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
hello world: 0
|
||||
hello world: 1
|
||||
hello world: 2
|
||||
@@ -3088,7 +3088,7 @@ hello
|
||||
|
||||
In [19]:
|
||||
|
||||
```
|
||||
```py
|
||||
import os
|
||||
os.remove('newfile.txt')
|
||||
|
||||
@@ -3098,7 +3098,7 @@ os.remove('newfile.txt')
|
||||
|
||||
In [20]:
|
||||
|
||||
```
|
||||
```py
|
||||
f = open('newfile.txt','w')
|
||||
for i in range(3000):
|
||||
x = 1.0 / (i - 1000)
|
||||
@@ -3106,7 +3106,7 @@ for i in range(3000):
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
---------------------------------------------------------------------------
|
||||
ZeroDivisionError Traceback (most recent call last)
|
||||
<ipython-input-20-8dc1cc9b29ec> in <module>()
|
||||
@@ -3122,7 +3122,7 @@ ZeroDivisionError: float division by zero
|
||||
|
||||
In [21]:
|
||||
|
||||
```
|
||||
```py
|
||||
g = open('newfile.txt', 'r')
|
||||
print g.read()
|
||||
f.close()
|
||||
@@ -3130,7 +3130,7 @@ g.close()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
hello world: 0
|
||||
hello world: 1
|
||||
hello world: 2
|
||||
@@ -4109,7 +4109,7 @@ hell
|
||||
|
||||
In [22]:
|
||||
|
||||
```
|
||||
```py
|
||||
f = open('newfile.txt','w')
|
||||
try:
|
||||
for i in range(3000):
|
||||
@@ -4122,21 +4122,21 @@ finally:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
something bad happened
|
||||
|
||||
```
|
||||
|
||||
In [23]:
|
||||
|
||||
```
|
||||
```py
|
||||
g = open('newfile.txt', 'r')
|
||||
print g.read()
|
||||
g.close()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
hello world: 0
|
||||
hello world: 1
|
||||
hello world: 2
|
||||
@@ -5146,7 +5146,7 @@ hello world: 999
|
||||
|
||||
In [24]:
|
||||
|
||||
```
|
||||
```py
|
||||
with open('newfile.txt','w') as f:
|
||||
for i in range(3000):
|
||||
x = 1.0 / (i - 1000)
|
||||
@@ -5154,7 +5154,7 @@ with open('newfile.txt','w') as f:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
---------------------------------------------------------------------------
|
||||
ZeroDivisionError Traceback (most recent call last)
|
||||
<ipython-input-24-9d2a70065b27> in <module>()
|
||||
@@ -5170,14 +5170,14 @@ ZeroDivisionError: float division by zero
|
||||
|
||||
In [25]:
|
||||
|
||||
```
|
||||
```py
|
||||
g = open('newfile.txt', 'r')
|
||||
print g.read()
|
||||
g.close()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
hello world: 0
|
||||
hello world: 1
|
||||
hello world: 2
|
||||
@@ -6185,7 +6185,7 @@ hello world: 999
|
||||
|
||||
In [26]:
|
||||
|
||||
```
|
||||
```py
|
||||
import os
|
||||
os.remove('newfile.txt')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user