python file does not exist_文件和异常——python从编程入门到实践

从文件中读取数据

1. 读取整个文件

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

1713315-20190630203347499-842132753.png

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

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 infile_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 inlines: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 inlines:

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 inlines:

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 inlines:

pi_string= pi_string +line.strip()

birthday= input('Enter your birthday, in the form mmddyy:')if birthday inpi_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

print(3/0)

ZeroDivisionError: division by zero

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

2. 使用 try - except 代码块

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

try:print(3/0)exceptZeroDivisionError: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.")whileTrue:

first_number= input("\nFirst number:")if first_number == 'q':breaksecond_number= input("Second number:")if second_number == 'q':breakanswer= int(first_number)/int(second_number)print(answer)

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

Traceback (most recent call last):

File"division.py", line 12, in 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.")whileTrue:

first_number= input("\nFirst number:")if first_number == 'q':breaksecond_number= input("Second number:")if second_number == 'q':break

try:

answer= int(first_number)/int(second_number)exceptZeroDivisionError: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:3Second number: 0

You can't divide by zero!

First number:4Second number:2

2.0First 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 with open(filename) as f_obj:

FileNotFoundError: [Errno2] No such file or directory: 'alice.txt'

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

filename = 'alice.txt'

try:

with open(filename) as f_obj:

contents=f_obj.read()exceptFileNotFoundError:

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()exceptFileNotFoundError:

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. 使用多个文件

defcount_words(filename):"""计算一个文件大致包含多少个单词"""

try:

with open(filename) as f_obj:

contents=f_obj.read()exceptFileNotFoundError:

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 infilenames:

count_words(filename)

运行结果:

The file A-Case-of-Sunburn.txt has about 9416words.

Sorry, the file moby_dick.txt doesnotexist.

The file The-Lay-of-the-Nibelung-Men.txt has about 124918 words.

8. 失败时一声不吭

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

try:

...exceptFileNotFoundError:pass

else:

...

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

存储数据

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

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

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

importjson

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. 保存和读取用户生成的数据

importjson#存储用户的名字

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 isyour name? ela

We'll remember you when you come back, ela!

Welcome back, ela!

优化上述代码:

importjson#若存储了用户名就加载;否则提示用户输入并存储

filename = 'username.json'

try:

with open(filename) as f_obj:

username=json.load(f_obj)exceptFileNotFoundError:

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. 重构

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

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

importjsondefget_stored_username():"""如果存储了用户名,就获取它"""filename= 'username.json'

try:

with open(filename) as f_obj:

username=json.load(f_obj)exceptFileNotFoundError:returnNoneelse:returnusernamedefget_new_username():"""提示用户输入用户名"""username= input('What is your name?')

filename= "username.json"with open(filename,'w') as f_obj:

json.dump(username, f_obj)returnusernamedefgreet_user():"""问候用户,并指出其名字"""username=get_stored_username()ifusername:print("Welcome back," + username + "!")else:

username=get_new_username()print("We'll remember you when you come back," + username + "!")

greet_user()

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

loading.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值