当前位置: 移动技术网 > IT编程>脚本编程>Python > 文件和异常——python从编程入门到实践

文件和异常——python从编程入门到实践

2019年06月30日  | 移动技术网IT编程  | 我要评论

免费服装代理,vs岚120628,闪婚不闪离

从文件中读取数据

1. 读取整个文件

要读取文件,首先来创建一个文件:

然后打开并读取这个文件,再将其内容显示到屏幕上:

file_reader.py

with open('pi_digits.txt') as file_object:
    contents = file_object.read()
    print(contents)

解读上述代码:

  open( ) -> 要以任何方式使用文件,都首先得打开文件,这样才能访问它,此时就需要用到函数open(),该函数只接受一个参数:要打开文件的名称,同时返回表示文件的对象。

  with: 不再需要访问文件后调用 close( ) 将其关闭。

  read( ) ->读取文件中的全部内容。

运行结果:

3.1415926535
  8979323946
  2643383278

2. 文件路径

要让python打开不与程序文件位于同一目录中的文件,需要提供文件路径,让python到系统的特定位置去查找。

  文件路径的表示:1. 相对路径 -> 文件相对于当前运行程序所在的目录。eg. 在程序所在文件夹c:\users\yxf\desktop\python_pycharm新建一个文件夹text_file用于存储文件pi_digits.txt,此时就需要这样编写代码:

with open('text_files\pi_digits.txt') as file_object:

          2. 绝对文件路径 -> 文件所在完整路径。绝对路径比相对路径更长,故可将路径存储在一个变量中,再将变量传递给 open( ):

file_path = r'c:\users\yxf\desktop\python_pycharm\text_files\pi_digits.txt'
with open(file_path) as file_object:

由于文件路径中使用的是反斜杠,在python中被视为转义字符,故需要在开头的单引号前加上r。

3. 逐行读取

每次以一行的方式检查文件:

file_name = 'pi_digits.txt'

with open(file_name) as file_object:
    for line in file_object:
        print(line)

   运行结果:

3.1415926535

  8979323946

  2643383278

  通过对文件对象使用for循环来遍历文件中的每一行,但运行结果显示的每一行后边多了空白行,这是为什么呢?文件中每行的末尾都有一个看不见的换行符,而print语句也会加上一个换行符。为消除这些空白行,可在print语句中使用 rstrip( ):

print(line.rstrip())

  这样输出与文件内容就完全相同了。

4. 创建一个包含文件各行内容的列表

file_name = 'pi_digits.txt'

with open(file_name) as file_object:
    lines = file_object.readlines()

for line in lines:
    print(line.rstrip())

  方法readlines():从文件中读取每一行,并将其存储在列表中。

5. 使用文件的内容

file_name = 'pi_digits.txt'

with open(file_name) as file_object:
    lines = file_object.readlines()    # 将文件内容存储在列表中

pi_string = ''    # 新建一个空字符串
for line in lines:
    pi_string = pi_string + line.rstrip()    # 删除空白行并转换为字符串

print(pi_string)    # 打印字符串
print(len(pi_string))    # 打印字符串长度

  运行结果:

3.1415926535  8979323946  2643383278
36

   运行结果中包含了位于每行左边的空格,为删除这些空格,可使用 strip() 而不是 rstrip(),运行可得:

3.141592653589793239462643383278
32

6. 包含一百万位的大型数据

  一百万位的文件下载过慢,就复制使用了其中的一小部分,并打印到小数点后的50位:

file_name = 'pi_xx_digits.txt'

with open(file_name) as file_object:
    lines = file_object.readlines()  # 将文件内容存储在列表中

pi_string = ''  # 新建一个空字符串
for line in lines:
    pi_string = pi_string + line.strip()  # 删除空白行并转换为字符串

print(pi_string[: 52])  # 打印字符串
print(len(pi_string))  # 打印字符串长度

  运行结果:

3.14159265358979323846264338327950288419716939937510
1483

  由运行结果可知保存了小数点后的1481位在文件 pi_xx_digits.txt 中。

7. 圆周率中包含你的生日吗

  可以检测圆周率值的前1483位中是否包含自己的生日:

file_name = 'pi_xx_digits.txt'

with open(file_name) as file_object:
    lines = file_object.readlines()

pi_string = ''
for line in lines:
    pi_string = pi_string + line.strip()

birthday = input('enter your birthday, in the form mmddyy: ')
if birthday in pi_string:
    print('your birthday appears in the first 1483 digits of pi!')
else:
    print('your birthday does not appears in the first 1483 digits of pi.')

写入文件

  保存数据最简单的方式之一就是将其写入到文件中。通过将输出写入文件,即便关闭包含程序输出的终端窗口,这些输出依然存在:可以查看、分享或读取到内存进行处理。

1. 写入空文件

  将一条消息存储在文件中:

filename = 'programming.txt'

with open(filename, 'w') as file_object:
    file_object.write("i love programming.")

  解读代码:调用 open() 时提供了两个实参,第一个实参是要打开文件的名称;第二个实参('w')表示以写入模式打开这个文件。

    打开文件时,可指定读取模式('r')、写入模式('w')、附加模式('a')、读取和写入模式('r+')。

    省略模式实参,默认以只读模式打开文件。

    写入文件不存在时,函数 open() 将自动创建;但若指定文件已经存在,python将在返回文件对象前清空该文件,即新的写入内容会覆盖旧的。

  运行程序之后:会在程序文件所在目录发现新增了一个名为 programming.txt 的文件,打开该文件将看到其中包含如下的内容:

i love programming.

  note: python中只能将字符串写入文本文件。要存储数据时,必须先使用函数将其转换为字符串格式。

2. 写入多行

filename = 'programming.txt'

with open(filename, 'w') as file_object:
    file_object.write("i love programming.\n")  # 写入第一行内容,为避免内容挤在一起,用‘\n’换行
    file_object.write("i love creating new games.\n")  # 写入第二行

  像显示到终端的输出一样,还可以使用空格、制表符和空行来设置输出格式。

3. 附加到文件

filename = 'programming.txt'

with open(filename, 'a') as file_object:
    file_object.write("i also love finding meaning in large datasets.\n")
    file_object.write("i love creating apps that can run in a browser.\n")

  以附加模式打开文件,若文件本存在,将写入内容添加到文件末尾;文件不存在,则新创建一个写入内容。

异常

  每当发生让python不知所措的错误的时候,都会创建一个异常对象。如果编写了处理处理改异常的代码,程序将继续运行,如果未对异常处理,程序将停止,并显示一个traceback,其中包含有关异常的报告。

  异常使用 try - except 代码块处理,执行指定操作的同时告诉python发生异常了怎么办。

1. 处理zerodivisionerror异常

print(3/0)

  被除数时不能为0的,但在python中为0了会怎么呢?

traceback (most recent call last):
  file "division.py", line 1, in <module>
    print(3/0)
zerodivisionerror: division by zero

  python停止运行,并指出引发了哪种异常。发生异常也希望程序能继续运行怎么办呢?

2. 使用 try - except 代码块

  当有可能发生错误时,可编写一个 try - except 代码块来处理可能引发的异常:

try:
    print(3/0)
except zerodivisionerror:
    print("you can't divide by zero!")

  运行结果:

you can't divide by zero!

3. 使用异常避免崩溃

  发生错误时,如果程序还有工作没完成,妥善处理错误就尤为重要。

# 一个只执行除法运算的简单计算器
print("give me two numbers, and i'll divide them.")
print("enter 'q' to quit.")

while true:
    first_number = input("\nfirst number: ")
    if first_number == 'q':
        break
    second_number = input("second number: ")
    if second_number == 'q':
        break
    answer = int(first_number)/int(second_number)
    print(answer)

   如果用户给第二个数字输入0,就会出现:

traceback (most recent call last):
  file "division.py", line 12, in <module>
    answer = int(first_number)/int(second_number)
zerodivisionerror: division by zero

   这样的信息会把不懂技术的用户搞糊;会对怀有恶意的用户暴露重要信息。

4. else代码块

# 一个只执行除法运算的简单计算器
print("give me two numbers, and i'll divide them.")
print("enter 'q' to quit.")

while true:
    first_number = input("\nfirst number: ")
    if first_number == 'q':
        break
    second_number = input("second number: ")
    if second_number == 'q':
        break
    try:
        answer = int(first_number)/int(second_number)
    except zerodivisionerror:
        print("you can't divide by zero!")
    else:
        print(answer)

  运行结果:

give me two numbers, and i'll divide them.
enter 'q' to quit.

first number: 3
second number: 0
you can't divide by zero!

first number: 4
second number: 2
2.0

first number: q

  尝试执行 try 代码块中的代码:若引发了异常,执行except代码块的代码;若没有发生异常,执行else代码块。

5. 处理filenotfounderror异常

  使用文件时,最常见的问题就是找不到文件:文件可能在其他地方、文件名可能不对或者文件就不存在。尝试读取一个不存在的文件:

filename = 'alice.txt'

with open(filename) as f_obj:
    contents = f_obj.read()

  运行结果:

traceback (most recent call last):
  file "alice.py", line 3, in <module>
    with open(filename) as f_obj:
filenotfounderror: [errno 2] no such file or directory: 'alice.txt'

  上述代码运行错误是函数open()导致的,因此要处理这个错误,必须把try语句放在包含open()的代码前:

filename = 'alice.txt'

try:
    with open(filename) as f_obj:
        contents = f_obj.read()

except filenotfounderror:
    msg = "sorry, the file " + filename + " does not exist."
    print(msg)

  运行结果:

sorry, the file alice.txt does not exist.

  告知用户文件不存在。

6. 分析文本

  分析包含整本书的文本文件: 在网站http://www.gutenberg.org/下载文学文本the lay of the nibelung men.txt,提取文本,并尝试计算包含了多少个单词:

filename = 'the-lay-of-the-nibelung-men.txt'
try:
    with open(filename) as f_obj:
        contents = f_obj.read()

except filenotfounderror:
    msg = "sorry, the file " + filename + " does not exist."
    print(msg)

else:
    # 计算文件大致包含多少个单词
    words = contents.split()  # 以空格为分隔符,将字符串分拆成多个部分,并将这些部分存储在一个列表中
    num_words = len(words)
    print("the file " + filename + " has about " + str(num_words) + " words.")

  运行结果:

the file the-lay-of-the-nibelung-men.txt has about 124918 words.

 7. 使用多个文件

def count_words(filename):
    """计算一个文件大致包含多少个单词"""
    try:
        with open(filename) as f_obj:
            contents = f_obj.read()
    except filenotfounderror:
        msg = "sorry, the file " + filename + " does not exist."
        print(msg)
    else:
        # 计算文件大致包含多少个单词
        words = contents.split()
        num_words = len(words)
        print("the file " + filename + " has about " + str(num_words) + " words.")

  定义一个可以计算文件有多少单词的函数,之后就可调用该函数来计算多个文件的:

filenames = ['a-case-of-sunburn.txt', 'moby_dick.txt', 'the-lay-of-the-nibelung-men.txt']
for filename in filenames:
    count_words(filename)

  运行结果:

the file a-case-of-sunburn.txt has about 9416 words.
sorry, the file moby_dick.txt does not exist.
the file the-lay-of-the-nibelung-men.txt has about 124918 words.

8. 失败时一声不吭

  若希望在不捕获到异常时一声不吭,继续执行:

try:
     ...   
except filenotfounderror:
    pass
else:
    ...

  在捕获到异常时,执行pass语句。

存储数据

  很多程序都要求用户输入某种信息,程序把用户提供的信息存储在列表和字典等数据结构中。用户关闭程序时,就要保存提供的信息,一种简单的方式就是使用模块json来存储数据。

  模块json能将简单的python数据结构存储到文件中,并在程序再次运转时加载该文件中的数据。还可以使用json在python程序之间分享数据,与使用其他编程语言的人分享。

1. 使用json.dump( )和json.load( )

import json

numbers = [2, 3, 5, 7, 11, 13]

filename = 'number.json'
with open(filename, 'w') as f_ojb:  # 以写入模式打开文件
    json.dump(numbers, f_ojb)  # 使用函数json.dump()将列表存储到文件中

with open(filename) as f_ojb:
    nums = json.load(f_ojb)  # 使用函数json.load()将这个列表读取到内存中

print(nums)  # 打印读取到内存中的列表,比较是否与存入的列表相同

   运行结果:

[2, 3, 5, 7, 11, 13]

2. 保存和读取用户生成的数据

import json

# 存储用户的名字
username = input('what is your name? ')
filename = 'username.json'
with open(filename, 'w') as f_obj:
    json.dump(username, f_obj)  # 存储用户名与username.json文件中
    print("we'll remember you when you come back, " + username + "!")

# 向名字被存储的用户发出问候
with open(filename) as f_obj:
    un = json.load(f_obj)
    print("\nwelcome back, " + un + "!")

  运行结果:

what is your name? ela
we'll remember you when you come back, ela!

welcome back, ela!

  优化上述代码:

import json

# 若存储了用户名就加载;否则提示用户输入并存储
filename = 'username.json'
try:
    with open(filename) as f_obj:
        username = json.load(f_obj)
except filenotfounderror:
    username = input('what is your name? ')
    with open(filename, 'w') as f_obj:
        json.dump(username, f_obj)
        print("we'll remember you when you come back, " + username + "!")
else:
    print("\nwelcome back, " + username + "!")

  运行结果:

welcome back, ela!

3. 重构

  代码可以运行,但也可以做进一步改进——将代码划分成一些列完成具体工作的函数:这个过程称为重构。

  目的:让代码更清晰、易于理解、易扩展。

import json


def get_stored_username():
    """如果存储了用户名,就获取它"""
    filename = 'username.json'
    try:
        with open(filename) as f_obj:
            username = json.load(f_obj)
    except filenotfounderror:
        return none
    else:
        return username


def get_new_username():
    """提示用户输入用户名"""
    username = input('what is your name? ')
    filename = "username.json"
    with open(filename, 'w') as f_obj:
        json.dump(username, f_obj)
    return username


def greet_user():
    """问候用户,并指出其名字"""
    username = get_stored_username()
    if username:
        print("welcome back, " + username + "!")
    else:
        username = get_new_username()
        print("we'll remember you when you come back, " + username + "!")


greet_user()

  敲代码的时候忘了最后一行greet_user(),就说怎么运行程序都么有结果,哈哈哈!!!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网