一.常見的模式
二.例子如下
# r模式
file=open('haha.html','r',encoding='gbk')
f=file.read()
print(f)
file.close()
#?w模式
file=open('wenjian.txt','w')
file.write('窗前明月光')
file.close()
#?a模式
file=open('wenjian.txt','a')
file.write('窗前明月光,疑是地上霜贿堰。')
file.close()
#?w+模式
file=open('wenjian.txt','w+')
file.write('舉頭望明月沾瓦,')
file.seek(0)
a=file.read()
print(a)
file.close()
#?r+模式
file=open('wenjian.txt','r+')
file.write('低頭思故鄉(xiāng)')
file.seek(0)
a=file.read()
print(a)
file.close()
#?a+模式
file=open('haha.html','a+')
file.write('長頸鹿的脖子長')
file.seek(0)
a=file.read()
print(a)
file.close()
#?rb模式
file=open('haha.html','rb')
a=file.read()
print(a.decode('gbk'))
file.close()
#?wb模式
file=open('haha.html','wb')
a='夾著火車上面包'
a=a.encode('gbk')
file.write(a)
file.close()
#?ab模式
file=open('haha.html','ab')
a='夾著火車上面包'
a=a.encode('gbk')
file.write(a)
file.close()
#?rb+模式
file=open('haha.html','rb+')
a='三人行必有我?guī)?
a=a.encode('gbk')
file.write(a)
file.seek(0)
file.read()
file.close()
#?wb+模式
file=open('haha.html','wb+')
a='三人行必有我?guī)?1'
a=a.encode('gbk')
file.write(a)
file.seek(0)
file.read()
file.close()
'''
#?ab+模式
file=open('haha.html','wb+')
a='三人行必有我?guī)?11'
a=a.encode('gbk')
file.write(a)
file.seek(0)
file.read()
file.close()
注意:
.write()模式,·如果文件不存在那么創(chuàng)建,如果存在那么就先清空,然后寫入數(shù)據(jù)。
三.路徑缭保。a=f.write('hello world, i am here!')? print(a) 這樣寫返回值是長度。
三.路徑問題
在windows下:
四.讀數(shù)據(jù)
(一) readline:
file=open('wenjian.txt','r+')
a=file.write('窗前明月光,\n疑是地上霜,\n')
print(file.readline())
print(file.readline())
file.close()
或者:
file=open('wenjian.txt','r+')
a=file.write('窗前明月光,\n疑是地上霜,\n')
n=1
for i in a :
print(%s:%s)%(n,i)
n+=1
file.close()
(二) readlines:
f = open('test.txt','r')
content?=?f.readlines()
print(type(content))
i=1
fortempincontent:
print("%d:%s"%(i,?temp))
i+=1
f.close()
五.獲取文件位置(tell())
f = open("test.txt","r")
str?=?f.read(3)
position = f.tell()
print("當(dāng)前文件位置:?",?position)
六.定位文件位置(seek())
1.from:方向
a)0:表示文件開頭(python3)
b)1:表示當(dāng)前位置(python2)
c)2:表示文件末尾(python2)
f = open("test.txt","r")
str?=?f.read(30)
print("讀取的數(shù)據(jù)是:?",?str)
#查找當(dāng)前位置
position?=?f.tell()
print("當(dāng)前文件位置:?",?position)
#重新設(shè)置位置
f.seek(5,0)
#查找當(dāng)前位置
position?=?f.tell()
print("當(dāng)前文件位置:?",?position)
f.close()
四.os模塊
(一):文件的重命名蝙茶、刪除
(二):創(chuàng)建文件夾
(三)獲取當(dāng)前目錄
importos
os.getcwd()
(四)改變默認(rèn)目錄
importos
os.chdir("../")
(五)改變默認(rèn)目錄
importos
os.listdir("./")
(六)刪除文件夾
importos
os.rmdir("張三")
import?shutil
os.rmtree(‘m’)
五.os.path模塊
os.path.abspath(path) #返回絕對路徑
os.path.split(path)
將path分割成目錄和文件名二元組返回艺骂。
>>> os.path.split('c:\\csv\\test.csv')
('c:\\csv', 'test.csv')
>>> os.path.split('c:\\csv\\')
('c:\\csv', '')
os.path.dirname(path)
返回path的目錄。其實就是os.path.split(path)的第一個元素隆夯。
>>> os.path.dirname('c:\\csv\test.csv')
'c:\\'
>>> os.path.dirname('c:\\csv')
'c:\\'
os.path.exists(path)
如果path存在钳恕,返回True;如果path不存在蹄衷,返回False忧额。
>>> os.path.exists('c:\\')
True
>>> os.path.exists('c:\\csv\\test.csv')
False
os.path.isabs(path)
如果path是絕對路徑,返回True愧口。
os.path.isfile(path)
如果path是一個存在的文件睦番,返回True。否則返回False耍属。
>>> os.path.isfile('c:\\boot.ini')
True
>>> os.path.isfile('c:\\csv\\test.csv')
False
>>> os.path.isfile('c:\\csv\\')
False
os.path.isdir(path)
如果path是一個存在的目錄托嚣,則返回True。否則返回False厚骗。
>>> os.path.isdir('c:\\')
True
>>> os.path.isdir('c:\\csv\\')
False
>>> os.path.isdir('c:\\windows\\test.csv')
False