
-
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/540189.html
標籤:其他
