本文實例講述了python打開文件并獲取文件相關屬性的方法。分享給大家供大家參考。具體分析如下:
下面的代碼通過open函數打開文件,并輸出文件名、打開狀態、打開模式等屬性
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 |
輸出結果:
1
2
3
4
|
Name of the file : foo.txt Closed or not : False Opening mode : wb Softspace flag : 0 |
希望本文所述對大家的Python程序設計有所幫助。