Python 基礎(chǔ)教程
os.open() 方法用于打開一個(gè)文件,并且設(shè)置需要的打開選項(xiàng),模式參數(shù)mode參數(shù)是可選的,默認(rèn)為 0777。
open()方法語法格式如下:
os.open(file, flags[, mode]);
file -- 要打開的文件
flags -- 該參數(shù)可以是以下選項(xiàng),多個(gè)使用 "|" 隔開:
mode -- 類似 chmod()。
返回新打開文件的描述符。
以下實(shí)例演示了 open() 方法的使用:
#!/usr/bin/python # -*- coding: UTF-8 -*- import os, sys # 打開文件 fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) # 寫入字符串 os.write(fd, "This is test") # 關(guān)閉文件 os.close( fd ) print "關(guān)閉文件成功!!"
執(zhí)行以上程序輸出結(jié)果為:
關(guān)閉文件成功!!其他擴(kuò)展