修改檔案(通常使用第二種)

2022-09-29 15:27:11 字數 1054 閱讀 6007

#

1. 簡單直接粗暴的方式

#f = open("stus.txt",encoding="utf-8")

#content = f.read()

#content = content.upper() #把字母都變成大寫

#f.close()##

f = open("stus.txt",'w',encoding="utf-8") #w模式開啟,檔案被清空

#f.write(content)

#f.close()##

f = open("stus.txt","a+",encoding="utf-8")

#f.seek(0)

#content = f.read()

#f.seek(0)

#f.truncate() #刪除檔案的內容,從指標的位置開始刪除

## print(content.split())

#for stu in content.split():

#stu = stu.capitalize() #把首字母大寫

## f.write(stu)

#f.write(stu+"\n")

#2.一行一行處理

import

osf = open("

stus.txt

",encoding="

utf-8")

f2 = open("

stus_new.txt

","w

",encoding="

utf-8")

for line in

f: line =line.capitalize()

f2.write(line)

f.close()

f2.close()

os.remove(

"stus.txt

") #

刪除原檔案

os.rename("

stus_new.txt

","stus.txt

") #

把新檔案改名成原檔案

Qt 第二種執行緒

流程 工程目錄 新增在這裡作為全域性 namespace ui class mywidget public qwidget endif mywidget h mythread.cpp include mythread.h include 待會用到sleep函式 include mythread my...

分頁的第二種方式

優點 1.十分簡潔 缺點 1.不能及時響應資料庫中變化 2.重複查詢較多,每獲取乙個屬性 總頁數 都要查詢資料庫 將某一頁的資料直接封裝在此類中,調乙個方法便可獲取資料。public class fengye 計算頁數 public int gettotalpages int count,int p...

ios第二種post請求

請求部分 nsurl url nsurl urlwithstring porturl gt ajax validate5 2.建立乙個網路請求,分別設定請求方法 請求引數 nsmutableurlrequest request nsmutableurlrequest requestwithurl u...