1. 程式人生 > 其它 >第4章 4.3 讀取檔案

第4章 4.3 讀取檔案

一、讀取檔案,並通過篩選後打印出來:

(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ cat zen_of_python.txt
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ python
Python 3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('zen_of_python.txt') as file:
... for line in file:
... print(line)
...
The Zen of Python, by Tim Peters

 

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Special cases aren't special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should be one-- and preferably only one --obvious way to do it.

Although that way may not be obvious at first unless you're Dutch.

Now is better than never.

Although never is often better than *right* now.

If the implementation is hard to explain, it's a bad idea.

If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea -- let's do more of those!

>>>

去掉字串前後的空格才看起來比較舒服:

>>> with open('zen_of_python.txt', 'r') as file:
... for line in file:
... print(line.strip())
...
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

 

 

 


>>> with open('zen_of_python.txt', 'r') as file:
... for line in file:
... if 'should' in line.lower():
... print(line)
...
Errors should never pass silently.

There should be one-- and preferably only one --obvious way to do it.

>>>
>>> with open('zen_of_python.txt', 'rt') as file:
... for line in file:
... if 'better' in line.lower():
... print(line)
... break

...
Beautiful is better than ugly.

#'rt'指只讀的文字模式開啟。

 

 

 

 

with 上下文管理器是一種處理檔案非常方便的方法,因為它將在使用後(離開程式碼塊)關閉檔案,即使出現異常也是這樣。

另外,傳統的方式開啟檔案如下:

>>> file = open('zen_of_python.txt')
>>> content = file.read()
>>> print(content)
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

>>> file.close()