|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
看完了28讲的,做课后习题,一直不成功!大神给看看!~
习题是打开是mp3的文件,我按照代码写的!
- file = open('//users//apple//downloads//OpenMe.mp3')
- for i in file:
- print(i,end='')
- file.close()
复制代码
Traceback (most recent call last):
File "/Users/apple/Documents/python/filetest.py", line 2, in <module>
for i in file:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb3 in position 0: ordinal not in range(128)
一直报这样的错,有些文件可以打开,这个就是不行啊!~
PS:姿势不正确 可不能插入的哦~
1. see code
- In [1]: ls #mp3文件,linux命令
- 周艳泓 - 吓死宝宝了.mp3
- In [2]: pwd #我的当前路径,linux命令
- Out[2]: '/root'
- In [3]: f = open(r'/root/周艳泓 - 吓死宝宝了.mp3', 'rb') #以二进制模式打开,路径前加r可自动转义哦~
- In [4]: n = 1
- In [5]: for i in f.read(): #你没写read()哦~
- ...: while n < 5: #读取4行
- ...: print(i)
- ...: n += 1
- ...:
- 73
- 73
- 73
- 73
复制代码
|
|