欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

python中的文件讀寫操作

python中如何對文件進行讀寫操作?針對這個問題,今天小編總結這篇有關文件讀寫的文章,希望能幫助更多想解決這個問題的朋友找到更加簡單易行的辦法。

站在用戶的角度思考問題,與客戶深入溝通,找到圖們網(wǎng)站設計與圖們網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設計與互聯(lián)網(wǎng)技術結合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站設計、成都網(wǎng)站建設、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、申請域名、虛擬主機、企業(yè)郵箱。業(yè)務覆蓋圖們地區(qū)。

open(filemode='r'buffering=-1encoding=Noneerrors=Nonenewline=Noneclosefd=Trueopener=None)

CharacterMeaning
'r'open for reading (default)
'w'open for writing, truncating the file first
'x'open for exclusive creation, failing if the file already exists
'a'open for writing, appending to the end of the file if it exists
'b'binary mode
't'text mode (default)
'+'open a disk file for updating (reading and writing)
'U'universal newlines mode (deprecated)

The default mode is 'r' (open for reading text, synonym of 'rt'). For binary read-write access, the mode 'w+b' opens and truncates the file to 0 bytes. 'r+b' opens the file without truncation.

文本讀寫操作:open(), close(), read(), readlines(),  

一、普通操作,open(),read(),close()

#!/usr/bin/python
#coding=utf-8

import logging

try:
	f = open('/home/seeing-zynq/Documents/Temp/Test/mydict.py', 'r')
	print f.read();
	print 'read'
except Exception as e:
	logging.exception(e)
	print 'error'
	raise 
finally:
	if f:
		f.close()
		print 'OK'

運行結果:

#!/usr/bin/python
# -*- coding: utf-8 -*-

class Dict(dict):

    def __init__(self, **kw):
        super().__init__(**kw)

    def __getattr__(self, key):
        try:
            return self[key]
        except KeyError:
            raise AttributeError(r"'Dict' object has no attribute '%s'" % key)

    def __setattr__(self, key, value):
        self[key] = value





read
OK

二、read()完后自動close()

with open('/home/seeing-zynq/Documents/Temp/Test/mydict.py', 'r') as f:    
	print (f.read())

運行結果:

#!/usr/bin/python
# -*- coding: utf-8 -*-

class Dict(dict):

    def __init__(self, **kw):
        super().__init__(**kw)

    def __getattr__(self, key):
        try:
            return self[key]
        except KeyError:
            raise AttributeError(r"'Dict' object has no attribute '%s'" % key)

    def __setattr__(self, key, value):
        self[key] = value

三、為避免read()未知容量的大文件,保險起見用readlines().

print '------------------------------------'
print '-----------------------------------'
f = open('/home/seeing-zynq/Documents/Temp/Test/mydict.py', 'r')
for line in f.readlines():
    print(line.strip())  ##strip會將前面首字符前的空格去掉,造成行句沒有縮進
f.close()
print 'over'

運行結果:

------------------------------------
-----------------------------------
#!/usr/bin/python
# -*- coding: utf-8 -*-

class Dict(dict):

def __init__(self, **kw):
super().__init__(**kw)

def __getattr__(self, key):
try:
return self[key]
except KeyError:
raise AttributeError(r"'Dict' object has no attribute '%s'" % key)

def __setattr__(self, key, value):
self[key] = value




over

四、讀二進制文件,如圖片,視頻等

>>> f = open('/Users/michael/test.jpg', 'rb')
>>> f.read()
b'\xff\xd8\xff\xe1\x00\x18Exif\x00\x00...' # 十六進制表示的字節(jié)

五、write()

with open('/home/seeing-zynq/Documents/Temp/IO/a.txt', 'w') as f:   
    f.write('haha')

with open('/home/seeing-zynq/Documents/Temp/IO/a.txt', 'r') as f:  
    print (f.read())
"file.py" 37L, 758C wri

運行結果:

haha

看完上述內容,你們對python中的文件讀寫操作大概了解了嗎?如果想了解更多相關文章內容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站標題:python中的文件讀寫操作
本文來源:http://aaarwkj.com/article4/jjjoie.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、關鍵詞優(yōu)化企業(yè)建站、網(wǎng)站改版、用戶體驗域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設
熟女肥臀一区二区三区| 日本高清精品视频在线| 亚洲天堂福利视频网站| 亚洲国产日韩欧美一级| 成人激情视频在线观看| 成人黄色18免费网站| 精品女同一区二区三区网站| 91中文字幕国产日韩| 97色伦97色伦国产在线| 日本中文字幕在线一区| 久久精品国产一区二区三区91| 天堂av日韩在线播放| 日韩亚洲欧美国产另类| 人妻中文字幕av资源| 玩弄丰满熟妇班主任老师| 日韩精品欧美精品视频一区| 日本一区二区三区三州免费观看| 日韩欧美一区二区福利视频| 亚洲一区二区三区熟女av| 欧美日韩一级一区二区| 国内精品亚洲成av人片麻豆| 欧美精品欧美精品一区二区| 色中文字幕人妻诱惑制服| av一区二区中文字幕| 伊人激情久久综合中文字幕| 九九在线视频精品免费播放| 五月婷婷丁香六月在线综合| 国产又粗又猛又爽黄老大爷| 男同午夜视频在线观看| 国产三级传媒视频在线观看| 很黄无遮挡在线免费网站| 2022国产精品一区| 午夜影院免费在线观看五分钟| 国产原创av剧情在线播放| 91国产香蕉在线观看| 国产欧美日本综合一区| 国产精品日韩伦理一区二区| 五月天亚洲激情综合av| 日本中文字幕一区二区视频| 久热99在线视频免费观看| 激情五月婷婷我有我色|