Io——文件的操作

一.常見的模式


二.例子如下

# 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('低頭思故鄉')

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='三人行必有我師'

a=a.encode('gbk')

file.write(a)

file.seek(0)

file.read()

file.close()

#?wb+模式

file=open('haha.html','wb+')

a='三人行必有我師11'

a=a.encode('gbk')

file.write(a)

file.seek(0)

file.read()

file.close()

'''

#?ab+模式

file=open('haha.html','wb+')

a='三人行必有我師111'

a=a.encode('gbk')

file.write(a)

file.seek(0)

file.read()

file.close()


注意:

.write()模式,·如果文件不存在那么創建,如果存在那么就先清空,然后寫入數據。

三.路徑。a=f.write('hello world, i am here!')? print(a) 這樣寫返回值是長度。


三.路徑問題

在windows下:

四.讀數據

(一) 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("當前文件位置:?",?position)

六.定位文件位置(seek())

1.from:方向

a)0:表示文件開頭(python3)

b)1:表示當前位置(python2)

c)2:表示文件末尾(python2)

f = open("test.txt","r")

str?=?f.read(30)

print("讀取的數據是:?",?str)

#查找當前位置

position?=?f.tell()

print("當前文件位置:?",?position)

#重新設置位置

f.seek(5,0)

#查找當前位置

position?=?f.tell()

print("當前文件位置:?",?position)

f.close()

四.os模塊

(一):文件的重命名、刪除


(二):創建文件夾


(三)獲取當前目錄

importos

os.getcwd()

(四)改變默認目錄

importos

os.chdir("../")

(五)改變默認目錄

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容