本文實(shí)例講述了python打開文件并獲取文件相關(guān)屬性的方法。分享給大家供大家參考。具體分析如下:
下面的代碼通過open函數(shù)打開文件,并輸出文件名、打開狀態(tài)、打開模式等屬性
1
2
3
4
5
6
7
|
#!/usr/bin/python # Open a file fo = open ( "foo.txt" , "wb" ) print "Name of the file: " , fo.name print "Closed or not : " , fo.closed print "Opening mode : " , fo.mode print "Softspace flag : " , fo.softspace |
輸出結(jié)果:
1
2
3
4
|
Name of the file : foo.txt Closed or not : False Opening mode : wb Softspace flag : 0 |
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。