python主要的文件打開的幾種訪問模式html
模式能夠爲讀模式('r'
)、寫模式('w'
)或追加模式('a'
),固然還有rb、wb、ab、r+、w+、a+、rb+、wb+、ab+,然而實際從代碼上我也沒看出來差異在哪。。。python
w = file('testfile','a')
w.write("這裏是我新寫入的文字內容!!!!")
w.close()
r = file('testfile','r')
str = r.read()
r.close()
print str
這裏面咱們還看到file相關方法,read()、write()、close()除了這些還有readlines()、writelines()、seek()、next()等等,更多參考菜鳥網:http://www.runoob.com/python/file-methods.htmlspa
input和raw_input的差異code
#input,若是輸入的是2+3,那麼返回的是5
str = input("請輸入:");
print "你輸入的內容是: ", str
# raw_input,若是輸入的是2+3,那麼返回的仍是2+3
str = raw_input("請輸入:");
print "你輸入的內容是: ", str
*tip*另外,若是用input輸入字符串,輸入字符串的時候須要添加引號,raw_input輸入字符串不須要

文件的相關操做,python內置os模塊。能夠刪除文件,重命名等操做htm
import os
print os.name
print os.getcwd()
print os.listdir("d:/myStudy/python/base")
print os.rmdir('dirname')
更多os的方法參考菜鳥網:http://www.runoob.com/python/os-file-methods.html