1. 程式人生 > >Python 讀取目錄、文件

Python 讀取目錄、文件

list tor 文件的操作 for etc 文件路徑 unicode 修改 字符串

在讀文件的時候往往需要遍歷文件夾,python的os.path包含了很多文件、文件夾操作的方法。下面列出:

os.path.abspath(path) #返回絕對路徑 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回多個路徑中,所有path共有的最長的路徑。 os.path.dirname(path) #返回文件路徑 os.path.exists(path) #路徑存在則返回True,路徑損壞返回False os.path.lexists #路徑存在則返回True,路徑損壞也返回True os.path.expanduser(path) #把path中包含的"~"和"~user"轉換成用戶目錄
os.path.expandvars(path) #根據環境變量的值替換path中包含的”$name”和”${name}” os.path.getatime(path) #返回最後一次進入此path的時間。 os.path.getmtime(path) #返回在此path下最後一次修改的時間。 os.path.getctime(path) #返回path的大小 os.path.getsize(path) #返回文件大小,如果文件不存在就返回錯誤 os.path.isabs(path) #判斷是否為絕對路徑 os.path.isfile(path) #判斷路徑是否為文件
os.path.isdir(path) #判斷路徑是否為目錄 os.path.islink(path) #判斷路徑是否為鏈接 os.path.ismount(path) #判斷路徑是否為掛載點() os.path.join(path1[, path2[, ...]]) #把目錄和文件名合成一個路徑 os.path.normcase(path) #轉換path的大小寫和斜杠 os.path.normpath(path) #規範path字符串形式 os.path.realpath(path) #返回path的真實路徑 os.path.relpath(path[, start]) #從start開始計算相對路徑
os.path.samefile(path1, path2) #判斷目錄或文件是否相同 os.path.sameopenfile(fp1, fp2) #判斷fp1和fp2是否指向同一文件 os.path.samestat(stat1, stat2) #判斷stat tuple stat1和stat2是否指向同一個文件 os.path.split(path) #把路徑分割成dirname和basename,返回一個元組 os.path.splitdrive(path) #一般用在windows下,返回驅動器名和路徑組成的元組 os.path.splitext(path) #分割路徑,返回路徑名和文件擴展名的元組 os.path.splitunc(path) #把路徑分割為加載點與文件 os.path.walk(path, visit, arg) #遍歷path,進入每個目錄都調用visit函數,visit函數必須有3個參數(arg, dirname, names),dirname表示當前目錄的目錄名,names代表當前目錄下的所有文件名,args則為walk的第三個參數 os.path.supports_unicode_filenames #設置是否支持unicode路徑名 os.listdir(root_path) #列出root_path文件夾下所有的目錄與文件

def analyze_path(path):
    print("abspath:", os.path.abspath(path))
    print("basename:", os.path.basename(path))
    print("dirname:", os.path.dirname(path))
    print("exists:", os.path.exists(path))
    print("atime:", os.path.getatime(path))
    print("normcase:", os.path.normcase(path))
    print("normpath:", os.path.normpath(path))
    print("realpath:", os.path.realpath(path))
    print("join:", os.path.join("F:\\test\\", os.path.basename(path)))
    print("splitdrive:", os.path.splitdrive(path))
    print("splitunc:", os.path.splitunc(path))

def main():
    path = "E:\\Users\\Administrator\\eclipse-workspace\\com.leagsoft\\test\\example.csv"
    analyze_path(path)
    

if __name__ == "__main__":
    main()

輸出:

abspath: E:\Users\Administrator\eclipse-workspace\com.leagsoft\test\example.csv
basename: example.csv
dirname: E:\Users\Administrator\eclipse-workspace\com.leagsoft\test
exists: True
atime: 1537200000.0
normcase: e:\users\administrator\eclipse-workspace\com.leagsoft\test\example.csv
normpath: E:\Users\Administrator\eclipse-workspace\com.leagsoft\test\example.csv
realpath: E:\Users\Administrator\eclipse-workspace\com.leagsoft\test\example.csv
join: F:\test\example.csv
splitdrive: (E:, \\Users\\Administrator\\eclipse-workspace\\com.leagsoft\\test\\example.csv)
splitunc: (‘‘, E:\\Users\\Administrator\\eclipse-workspace\\com.leagsoft\\test\\example.csv)

遍歷文件和目錄:

 rootdir = F:\data
 list = os.listdir(rootdir) #列出文件夾下所有的目錄與文件
 for i in range(0,len(list)):
        path = os.path.join(rootdir,list[i])
        if os.path.isfile(path):
               #你想對文件的操作

Reference:

[1] http://www.cnblogs.com/WonderHow/p/4403727.html

Python 讀取目錄、文件