python文件目錄操作之os模塊
2.1abspath返回絕對路徑import os ,syspath=os.getcwd()path2=os.path.abspath(’others.py’) #文件不存在也會返回全路徑,但不會創建文件print(path)print(path2) 2.2basename返回文件名import os ,syspath2=os.path.basename(r’E:pythonprojectAnacondatempothers.py’)#用法:從全路徑中取文件名稱print(path2) 2.3dirname返回路徑import os ,syspath2=os.path.dirname(r’E:pythonprojectAnacondatempothers.py’)#只返回全路徑中的路徑部分print(path2) 2.4exists判斷文件是否存在import os ,syspath2=os.path.exists(r’E:pythonprojectAnacondatempothers.py’)print(path2) #若存在返回true,否則返回false 2.5lexists判斷文件是否存在import os ,syspath2=os.path.lexists(r’E:pythonprojectAnacondatempothers.py’)print(path2) #若存在返回true,否則返回false 2.6expanduseros.path.expanduser(path) >>>print os.path.basename('D:SQAPSQAP Training.pdf')>>>D:SQAPSQAP Training.pdf 2.7expandvarsos.path.expandvars(path) #根據環境變量的值替換path中包含的'name'和'{name}'>>>print os.path.basename('D:SQAPSQAP Training.pdf')>>>D:SQAPSQAP Training.pdf 2.8getatime返回最后一次進入此path的時間import os ,syspath2=os.path.getatime(r’E:pythonprojectAnacondatemp’) #可以是文件夾,也可是文件print(path2) #結果返回:1488813625.5529294 2.9getmtime返回文件夾或文件最后修改時間import os ,syspath2=os.path.getmtime(r’E:pythonprojectAnacondatempothers.py’)print(path2) #結果:1488732500.9374976 3.0getctime文件最近訪問時間import os ,syspath2=os.path.getctime(r’E:pythonprojectAnacondatempothers.py’)print(path2) #結果:1480692429.5645697 3.1getsize返回文件大小import os ,syspath2=os.path.getsize(r’D:迅雷下載三少爺的劍.rmvb’)print(path2) #結果:1384146720B 3.2isabs判斷是否為絕對路徑import os ,syspath2=os.path.isabs(r’E:pythonprojectAnacondatempothers.py’)print(path2) #結果:True 3.3isfile判斷是否為文件import os ,syspath2=os.path.isfile(r’E:pythonprojectAnacondatempothers.py’)print(path2) #結果:True 3.4isdir判斷是否為目錄import os ,syspath2=os.path.isdir(r’E:pythonprojectAnacondatempothers.py’)print(path2) #結果:False 3.5join把目錄與文件合成全路徑import os ,syspath2=os.path.join(r’E:pythonprojectAnacondatemp’,r’others.py’)print(path2) #結果:E:pythonprojectAnacondatempothers.py 3.6normcase轉換path的大小寫和斜杠import os ,syspath2=os.path.normcase(r’E:/PYTHON/projectAnacondaTEMPothers.py’)print(path2) #結果:e:pythonprojectanacondatempothers.py 3.7samefile判斷目錄或文件是否相同import os ,syspath1=r’E:/PYTHON/projectAnacondaTEMPothers.py’path2=r’E:pythonprojectAnacondatempothers.py’result=os.path.samefile(path1,path2)print(result) #結果:true 3.8split路徑分割成dirname和basename,返回元組import os ,syspath1=r’E:PYTHONprojectAnacondaTEMPothers.py’result=os.path.split(path1)print(result) #結果(’E:PYTHONprojectAnacondaTEMP’, ’others.py’) 3.9splitext分割路徑,返回路徑名和文件擴展名的元組import os ,syspath1=r’E:PYTHONprojectAnacondaTEMPothers.py’result=os.path.splitext(path1)print(result)#結果返回元組:(’E:PYTHONprojectAnacondaTEMPothers’, ’.py’) 4.0os.path.walk遍歷文件夾os.path.walk(r'C:UsersAdministratorDesktop4',find_file,())os.walk()產生目錄樹下的目錄路徑和文件路徑,而os.path.walk()只產生文件路徑(是子目錄與文件的混合列表)。四、os常用操作
1.讀取當前路徑os.path.realpath(__file__)##py運行的位置注意與os.getcwd()的區別os.path.dirname(os.path.abspath(__file__)) 2.獲取文件的時間屬性os.path.getatime(file) 輸出文件訪問時間os.path.getctime(file) 輸出文件的創建時間os.path.getmtime(file) 輸出文件最近修改時間 import time import ostime.ctime(os.path.getatime(file))# linux顯示的是最近修改時間time.ctime(os.path.getmtime(file))time.ctime(os.path.getctime(file)) 3.os.listdir()以列表返回文件夾下所有文件和目錄# )以列表返回文件夾下所有文件和目錄,但不會返回子目錄的文件import os #get filepath=’/Users/juvo/Downloads/test’dirs = os.listdir(path)file_list=[]for file in dirs: print(1,file) if str(file).endswith(’.txt’):file_list.append(file)
到此這篇關于python文件目錄操作之os模塊的文章就介紹到這了,更多相關Python os模塊內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. 將properties文件的配置設置為整個Web應用的全局變量實現方法2. docker compose idea CreateProcess error=2 系統找不到指定的文件的問題3. 在Vue 中獲取下拉框的文本及選項值操作4. JS中的常見數組遍歷案例詳解(forEach, map, filter, sort, reduce, every)5. Python語言規范之Pylint的詳細用法6. python爬蟲利用代理池更換IP的方法步驟7. Vue+express+Socket實現聊天功能8. python中pandas.read_csv()函數的深入講解9. SpringBoot集成SSM、Dubbo、Redis、JSP的案例小結及思路講解10. JS算法題解旋轉數組方法示例
