首页 > 代码库 > 第十章文件和异常

第十章文件和异常

#1.
#A:使用关键字with后,会在不需要访问文件后将其关闭
#B:open(),默认以"r"的形式打开文件,close()分别用于关闭文件
#C:读取文件的方法:read()默认读取整个文件,可以指定读取字节。readline()读取一行,readlines()按行读取后存入列表,当传入读取字节数目后,则读取的内容可能是行的一部分
#D:python3将文件当做一个可迭代的类型,此方法比较好
#E:seek()设置开始读的位置, tell()得到当前开始读取位置

#Test.txt内容是12\r\n22\r\n32
with open("Test.txt") as pFile:
    buffFile = pFile.read()     #buffFile = ‘12\n22\n32‘
with open("Test.txt", "rb") as pFile:
    buffFile = pFile.read()     #buffFile = b‘12\r\n22\r\n32‘
with open("Test.txt", "rb") as pFile:
    buffFile = pFile.read(4)    #buffFile = b‘12\r\n

pFile = open("Test.txt")
buffFile = pFile.readline()     #buffFile = ‘12\n‘
pFile.close()

buffFile = ""
with open("Test.txt") as pFile:
    buffFile = pFile.readlines()#buffFile = [‘12\n‘, ‘22\n‘, ‘32\n‘]

pFile = open("Test.txt")
buffFile = ‘‘
for line in pFile:              #python3将文件当做一个可迭代的类型
    buffFile += line            #buffFile = ‘12\n22\n32‘
pFile.close()

pFile = open("Test.txt")
pFile.seek(1)
value = http://www.mamicode.com/pFile.tell()            #value = 1"Test.dat") as pFile:
    buffFile = pFile.read()     #buffFile = ‘1\n2\n3\n4‘
with open("Test.dat", "rb") as pFile:
    buffFile = pFile.read()     #buffFile = b‘1\r2\r\n3\n4‘
with open("Test.dat", "r", newline = "") as pFile:
    buffFile = pFile.read()     #buffFile = ‘1\r2\r\n3\n4‘

#TestWithNull.dat内容:‘1‘, 0x0d, ‘2‘, 0x0d, 0x0a, ‘3‘, 0x0a, ‘4‘, 0, 0
with open("TestWithNull.dat", "rb") as pFile:
    buffFile = pFile.read()     #buffFile = b‘1\r2\r\n3\n4\x00\x00‘

#3.
#A:write()用于写入文件
#B:writelines()用于写入多行
with open("Write.txt", "wb") as pFile:
    pFile.write(b‘1\r2\r\n3\n4\x00\x00‘)    #文件中内容:0x31 0x0d 0x32 0x0d 0x0a 0x33 0x0a 0x34 0x00 0x00
with open("Write.txt", "w") as pFile:
    pFile.write(‘1\r2\r\n3\n4\x00\x00‘)     #文件内容:0x31 0x0d 0x32 0x0d 0x0d 0x0a 0x33 0x0d 0x0a 0x34 0x00 0x00
with open("Write.txt", "w", newline = ‘‘) as pFile:
    pFile.write(‘1\r2\r\n3\n4\x00\x00‘)     #文件内容:0x31 0x0d 0x32 0x0d 0x0a 0x33 0x0a 0x34 0x00 0x00

with open("Write.txt", "w") as pFile:
#    pFile.write(b‘1\r2\r\n3\n4\x00\x00‘)   #运行时候报错:must be str, not bytes
    pass

value = http://www.mamicode.com/["123", ‘456‘]
with open("Write.txt", "w") as pFile:
#    pFile.write(value)                     #运行时候报错:must be str, not list
    pass

value = http://www.mamicode.com/["123", ‘456‘]
with open("Write.txt", "w") as pFile:
    pFile.writelines(value)                 #文件内容:123456

#4.
#A:python使用异常来管理程序执行期间发生的错误。每当发送错误的时候,都会创建一个异常对象,若编写了处理该异常的代码,则程序会继续运行,否则会报错
#B:split()根据字符串创建一个单词列表
#C:pass语句充当占位符,表示当前不做任何事

#value = http://www.mamicode.com/1 / 0                              #此处会出现异常"Error")                          #Error
else:
    print(‘Not Error‘)

try:
    value = http://www.mamicode.com/1 / 1"Error")
else:
    print(‘Not Error‘)                      #Not error

try:
    pFile = open("NotExitFile.dat")
except FileNotFoundError:
    print(‘File Not Find‘)                  #File Not Find
else:
    pFile.close()      
    
value = http://www.mamicode.com/‘hello world bad world‘"people" : [{"A" : 90}, {"B" : 80}, {"C" : 70}],"data" : 10}‘
jsonValue = http://www.mamicode.com/json.loads(value)               #jsonValue = {"people" : [{"A" : 90}, {"B" : 80}, {"C" : 70}],"data" : 10}
data = http://www.mamicode.com/jsonValue[‘people‘]                  #data = [{‘A‘: 90}, {‘B‘: 80}, {‘C‘: 70}]"people" : [{"A" : 90}, {"B" : 80}, {"C" : 70}],"data" : 10, "Test": "szn"}

value = http://www.mamicode.com/{"people" : [{"A" : 90}, {"B" : 80}, {"C" : 70}],"data" : 10};
json_str = json.dumps(value)                #json_str = ‘{"data": 10, "people": [{"A": 90}, {"B": 80}, {"C": 70}]}‘

with open("JsonFile.json", "w") as pFile:
    json.dump(value, pFile)                 #文件内容:{"data": 10, "people": [{"A": 90}, {"B": 80}, {"C": 70}]}

with open("JsonFile.json") as pFile:
    buff = json.load(pFile)                 #buff = ‘{"data": 10, "people": [{"A": 90}, {"B": 80}, {"C": 70}]}‘

value = http://www.mamicode.com/{1:2, ‘1‘:2}                        #value = {1: 2, ‘1‘: 2}"1": 2, "1": 2}‘
Test = json.loads(json_str)                 #Test = {‘1‘: 2}

  

第十章文件和异常