# 文件基本操作:打開文件,處理文件,關閉文件
the_file = open("sketch.txt", 'r')
line_content = the_file.readline() # 不指定readline的參數,讀取一整行
print(line_content)# 輸出了sketch.txt的第一行數據
E:\Github\python\env\Scripts\python.exe E:/Github/python/files_error/fileIO/fileIO.py
Man: Is this the right room for an argument?# 文件基本操作:打開文件,處理文件,關閉文件
the_file = open("sketch.txt", 'r')
line_content = the_file.readline(5) # 指定readline的參數為5,最多輸出了4個字符
print(line_content)# 輸出了4個字符
E:\Github\python\env\Scripts\python.exe E:/Github/python/files_error/fileIO/fileIO.py
Man:
2. 文件路徑的查詢,更改# 文件基本操作:打開文件,處理文件,關閉文件
import os
os.getcwd()
os.chdir('../fileIO')
the_file = open("sketch.txt", 'r')
for line_each in the_file:
line_content = the_file.readline()
print(line_content, end='')
the_file.close()E:\Github\python\env\Scripts\python.exe E:/Github/python/files_error/fileIO/fileIO.py
Other Man: I've told you once.
Other Man: Yes I have.
Other Man: Just now.
Other Man: Yes I did!
Other Man: I'm telling you, I did!
Other Man: Oh I'm sorry, is this a five minute argument, or the full half hour?
Other Man: Just the five minutes. Thank you.
Man: You most certainly did not!
3. 文件一些基本操作接口os.getcwd 類似於linux下的pwd,獲取當前工作路徑
# 文件基本操作:打開文件,處理文件,關閉文件
import os
os.getcwd()
os.chdir('../fileIO')
the_file = open("sketch.txt", 'r')
"""
for line_each in the_file:
line_content = the_file.readline()
print(line_content, end='')
"""
line_content = the_file.readline()
print(line_content, end='')
line_content = the_file.readline()
print(line_content, end='')
the_file.seek(0)
line_content = the_file.readline()
print(line_content, end='')
the_file.close()# 輸出結果可以看出,通過調用seek(0), 將文件指針調到最開始的地方
E:\Github\python\env\Scripts\python.exe E:/Github/python/files_error/fileIO/fileIO.py
Man: Is this the right room for an argument?
Other Man: I've told you once.
Man: Is this the right room for an argument?split("分割標識",最多允許分割次)
# 文件基本操作:打開文件,處理文件,關閉文件
import os
os.getcwd()
os.chdir('../fileIO')
the_file = open("sketch.txt", 'r')
# split用法
line_content = the_file.readline()
(role, line_spoken) = line_content.split(":")
print(role, end='\n')
print(line_spoken, end='\n')
the_file.close()# 輸出分割之後的數據
E:\Github\python\env\Scripts\python.exe E:/Github/python/files_error/fileIO/fileIO.py
Man
Is this the right room for an argument?find
# 文件基本操作:打開文件,處理文件,關閉文件
import os
os.getcwd()
os.chdir('../fileIO')
the_file = open("sketch.txt", 'r')
for line_each in the_file:
if not line_each.find(":") == -1:
(role, line_spoken) = line_each.split(":", 1)
print(role, end='=>')
print(line_spoken, end='')
the_file.close()E:\Github\python\env\Scripts\python.exe E:/Github/python/files_error/fileIO/fileIO.py
調用split方法時,由於存在在文本行中沒有特定的字符用於識別進行前後分割,如果調用split就會出錯基於上述問題,使用find方法先查找是否存在特定的分割字符,再調用split4. 異常處理:try/except機制
Man=> Is this the right room for an argument?
Other Man=> I've told you once.
Man=> No you haven't!
Other Man=> Yes I have.
Man=> When?
Other Man=> Just now.
Man=> No you didn't!
Other Man=> Yes I did!
Man=> You didn't!
Other Man=> I'm telling you, I did!
Man=> You did not!
Other Man=> Oh I'm sorry, is this a five minute argument, or the full half hour?
Man=> Ah! (taking out his wallet and paying) Just the five minutes.
Other Man=> Just the five minutes. Thank you.
Other Man=> Anyway, I did.
Man=> You most certainly did not!1. 慄子:打開一個不存在的文件進行讀取,出現IOError
the_file = open("sketch.txt", 'r')
for line_each in the_file:
if not line_each.find(":") == -1:
(role, line_spoken) = line_each.split(":", 1)
the_file.close()Traceback (most recent call last):
File "E:/Github/python/files_error/try_except/try_except.py", line 1, in <module>
the_file = open("sketch.txt", 'r')
IOError: [Errno 2] No such file or directory: 'sketch.txt'2. 如何處理IOError及其它的異常:try/except
try:
the_file = open("sketch.txt", 'r')
except IOError as err:
print("File error" + str(err))I:\Projects\wifi\Ai-Thinker\AiThinkerIDE_V1.0\msys32\mingw32\bin\python.exe E:/Github/python/files_error/try_except/try_except.py
File error[Errno 2] No such file or directory: 'sketch.txt'3. 如果文件打開失敗,然後調用close接口會出錯,使用locals()接口查看打開文件後的文件描述符是否存在來判斷文件是否打開成功
# encoding=UTF-8
# 打開文件時,如果文件不存在會出現IOError
try:
the_file = open("sketch.txt", 'r')
except IOError as err:
print("File error" + str(err))
# the_file的名字存在說明打開文件成功,否則打開失敗
if 'the_file' in locals():
the_file.close()# 打開文件失敗,判斷如果打開成功才關閉文件
I:\Projects\wifi\Ai-Thinker\AiThinkerIDE_V1.0\msys32\mingw32\bin\python.exe E:/Github/python/files_error/try_except/try_except.py
File error[Errno 2] No such file or directory: 'sketch.txt'