File對象測試數據的讀寫與操作 #def open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True): # known special case of open #file ...
-
File對象測試數據的讀寫與操作
#def open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True): # known special case of open #file: 操作的文件 #mode:打開這個文件的模式 'r' open for reading (default)讀取 - 預設值。打開文件進行讀取,如果文件不存在則報錯。 'w' open for writing, truncating the file first寫入 - 打開文件進行寫入,如果文件不存在則創建該文件。 'x' create a new file and open it for writing創建 - 創建指定的文件,如果文件存在則返回錯誤。 'a' open for writing, appending to the end of the file if it exists追加 - 打開供追加的文件,如果不存在則創建該文件。 'b' binary mode二進位模式 't' text mode (default)文本 - 預設值。文本模式。 '+' open a disk file for updating (reading and writing)打開磁碟文件進行更新(讀寫) 'U' universal newline mode (deprecated)通用換行模式(已棄用)
#buffering:可選參數,用於指定對文件做讀寫操作時,是否使用緩衝區
#encoding:手動設定打開文件時所使用的編碼格式,不同平臺的 ecoding 參數值也不同,以 Windows 為例,其預設為 cp936(實際上就是 GBK 編碼)
1.讀取
file = open("test0925.py")#預設為r res = file.read() print(res) #def open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True): # known special case of open #file: 操作的文件 #mode:打開這個文件的模式
file = open("test0925.py","r")#預設為r res = file.read() print(res)
2.寫入
#寫入 file = open("test0925.py","w",encoding="utf8")#w寫入,覆蓋源文檔的內容,亂碼時添加encoding="utf8" file.write("測試")
3.追加
#追加 file = open("test0925.py","a",encoding="utf8")#w寫入,覆蓋源文檔的內容,亂碼時添加encoding="utf8" file.write("aaaa")
4.按行讀取
#按行讀取 file = open("test0925.py","r",encoding="utf8") read = file.readline()#讀取一行 print(read)
5.讀取多行
#全部讀取 file = open("test0925.py","r",encoding="utf8") reads = file.readlines()#讀取所有行 print(reads)
-
OS操作文件夾/獲取路徑
1.文文件夾(目錄)
1.1.絕對路徑/相對路徑
#相對路徑/絕對路徑 第一種(絕對路徑表示法):C:\FIle\file two 第二種(相對路徑表示法):FIle two
1.2.新建目錄
import os #新建文件夾 os.mkdir("Eclipse")#
1.3.跨級新建目錄
import os #跨級新建目錄 os.mkdir("Eclipse/US")#跨級必須確保層級目錄存在,相對路徑
1.4.絕對路徑新建目錄
import os #絕對路徑新建目錄 os.mkdir("D:\\test_mkdir")#絕對路徑
1.5.刪除目錄
#目錄不為空時刪除失敗
import os #刪除文件夾 os.remove("demo.py")
import os #刪除 os.rmdir("m1/m2")
2.獲取路徑
2.1.獲取當前工作目錄
#獲取當前工作目錄 import os path = os.getcwd() print(path)#D:\File\python_project\demo\api
2.2.獲取當前文件所在的絕對路徑(具體到模塊名)
#獲取當前文件所在的絕對路徑 import os realpath = os.path.realpath(__file__)# __file__表示當前文件本身 print(realpath)#D:\File\python_project\demo\api\demo02.py
2.3.拼接路徑 +
#拼接路徑 import os new_path = os.getcwd()+"\\m1\\m2" # \\兩個反斜杠可以,\一個反斜杠可以,/一個斜杠也可以 print(new_path) #列印新的絕對路徑
2.4.拼接路徑 join
#路徑拼接 import os path = os.getcwd().join("test02") #拼接 print(path)
2.5.判斷是否為文件
#判斷文件 import os path = os.path.isfile(__file__)#判斷是否為文件 print(path)#True path2 = os.path.isfile(os.getcwd())#判斷當前路徑是否為文件 print(path2)#False
2.6.判斷是否為文件夾
#判斷文件夾 import os path = os.path.isdir(__file__)#判斷當前絕對路徑是否為文件夾 print(path)#False path2 = os.path.isdir(os.getcwd()) print(path2)#True
2.7.判斷路徑是否存在
#判斷路徑是否存在 import os bool = os.path.exists("D://download") print(bool) #True
2.8.列印指定路徑下所有文件和文件夾
#羅列當前路徑下所有的文件文件夾 import os list_path = os.listdir("C:\system_software\PyCharm 2019.1.3") print(list_path)#['bin', 'build.txt', 'debug-eggs', 'help', 'helpers', 'helpers-pro', 'index', 'jre64', 'lib', 'license', 'plugins', 'product-info.json', 'skeletons']
2.9.寫一個函數,判斷是否為目錄,如果時目錄羅列出所有的文件文件夾
#寫一個函數,判斷是否為目錄,如果時目錄羅列出所有的文件文件夾 import os def path_list(path): if os.path.exists(path) and os.path.isdir(path):#判斷是否為文件夾,是否存在 for item in (os.listdir(path)):#便利列表 new_path = path + "\\" + item if os.path.isdir(new_path):#是否為文件夾 print("D:{0}".format(new_path))#文件夾 elif os.path.isfile(new_path):#是否為文件 print("F:{0}".format(new_path))#文件 else: print("錯誤路徑:{0}".format(new_path)) path_list("C:\system_software\PyCharm 2019.1.3") 輸出:
D:C:\system_software\PyCharm 2019.1.3\bin F:C:\system_software\PyCharm 2019.1.3\build.txt D:C:\system_software\PyCharm 2019.1.3\debug-eggs D:C:\system_software\PyCharm 2019.1.3\help D:C:\system_software\PyCharm 2019.1.3\helpers D:C:\system_software\PyCharm 2019.1.3\helpers-pro D:C:\system_software\PyCharm 2019.1.3\index D:C:\system_software\PyCharm 2019.1.3\jre64 D:C:\system_software\PyCharm 2019.1.3\lib D:C:\system_software\PyCharm 2019.1.3\license D:C:\system_software\PyCharm 2019.1.3\plugins F:C:\system_software\PyCharm 2019.1.3\product-info.json D:C:\system_software\PyCharm 2019.1.3\skeletons