Python 文字讀寫操作

2021-10-23 22:08:13 字數 2394 閱讀 7829

mark下讀寫操作函式,省的下次再寫

#!/usr/bin/env python

# -*- coding: utf-8 -*-

'''@file : test.py.py

@author: piepis

@date : 2020/8/27 18:13

@desc :

'''import os

import re

# write "data" to file-filename

defwritefile

(filename, data,zhuijia=

false):

(path, name)

= os.path.split(filename)

if""

==path:

path =os.getcwd(

)else:if

not os.path.exists(path)

:# 判斷是否存在資料夾如果不存在則建立為資料夾

os.makedirs(path)

# makedirs 建立檔案時如果路徑不存在會建立這個路徑

if zhuijia:

with

open

(filename,

'a+'

)as f:

# f.write(data) # 加\n換行顯示

f.write(data +

'\n'

)# 加\n換行顯示

else

:with

open

(filename,

'w')

as f:

f.write(data +

'\n'

)# 加\n換行顯示

with

open

(os.path.join(path,

"namelist.md"),

'a+'

)as t:

t.write(name +

'\n'

)# read "data" from file-filename

defreadfile

(filename,lines=

false):

if lines:

with

open

(filename,

"r")

as f:

result =f.readlines(

)else

:with

open

(filename,

"r")

as f:

result =f.read(

)return result

if __name__ ==

'__main__'

: pattern= re.

compile

(r'handler.ksh(.*)dual'

) result = readfile(

"export.xml"

) tt =re.findall(pattern,result,flags=0)

t=print

(len

(tt)

)print

(tt)

for i in tt:

if"&"

in i:

"&","")

.strip())

print

(t)print

(len

(t))

tables_new_list =

for i in t:

if i not

in tables_new_list:

'\n'))

else

:print

(i) result1=readfile(

"tmp_list.sql"

,true

)# all_words = list(filter(lambda text: all([word in text for word in result]), result1))

ttt=

for ti in result1:

tc=ti.strip(

'\n'

).replace(

"_tmp",""

)str

(tc)))

print

("-----------------------"

)print

(ttt)

ttttttttt=

for i in ttt:

if i not

in ttttttttt:

else

:print

(i)

C 文字讀寫操作

檔案的讀寫操作 第二種方式是使用函式,但是這些函式只是能用於處理字元,對於其它資料型別則有些有心無力了,為此還是建議使用第一種方式,因為第一種方式過載了輸入輸出操作符,它可以處理所有內建資料型別。文字檔案它的每乙個位元組存放的是乙個ascii碼,代表乙個字元。二進位制檔案是將記憶體中的資料按照其在記...

Python 讀寫文字(open)

character meaning r open for reading default w open for writing,truncating the file first a b binary mode t text mode default open a disk file for upd...

Python基礎 文字讀寫

mode引數 預設為 r 2.1 read size 讀取檔案內容 f open tmp.txt r str f.read 5 print str f.close hello2.2 readline 讀取一行 f open tmp.txt r str f.readline print str f.c...