2019 05 16 檔案練習

2021-09-22 19:29:19 字數 1031 閱讀 1711

#encoding=utf-8

"""判斷 乙個檔案中有幾行含英文單詞

"""def jedgeword():

row=0

with open("c:\\users\\huhuili\\desktop\\python\\a.txt","r",encoding="utf8") as fp:

contents=fp.readlines()

for line in contents:

print(line,end="")

for i in line:

if i.isalpha():

#print(i)

row+=1

break

return row

print(jedgeword())

"""以r+方式,把一句話追加到檔案末尾,然後讀出所有檔案

"""def addcontent():

sentence="i am girl"

with open("c:\\users\\huhuili\\desktop\\python\\a.txt","r+",encoding="utf8") as fp:

fp.seek(0,2)#指標移動到檔案末尾

fp.write(sentence)

fp.seek(0,0)

print(fp.readlines())

print(addcontent())

"""把乙個檔案內容中的girl替換成queen

"""def replaceword():

word="queen"

with open("e:\\murphy\\a.txt","r+",encoding="utf8") as fp:

contents=fp.read()

contents=contents.replace("girl","queen")

fp.seek(0,0)

print(contents)

print(replaceword())

2019 05 25 檔案練習

encoding utf 8 檔案訪問,提示輸入數字 n 和檔案 f,然後顯示檔案 f 的前 n 行 import os def showfilerow path,n,f os.chdir path with open os.path.join path,f r as fp temp fp.read...

練習3 檔案許可權管理

1.新建使用者組 shengchan,caiwu,jishu 2.新建使用者要求如下 1 adminsc的初始組為shengchan 2 admincw的初始組為shengchan 3 adminjs的初始組為shengchan 4 tom是shengchan組的附加使用者 5 harry是caiw...

3 15 檔案處理練習2

inp file input 請輸入需要複製檔案的路徑 strip new file input 需要建立檔案副本位置 strip with open rf mode rb as f,open rf mode wb as f1 while true res f.read 1024 if len re...