網(wǎng)站首頁 編程語言 正文
1、需求
用戶輸入當前目錄下任意文件名,程序完成對該文件的備份功能。
備份文件名為xx[備份]后綴
,例如:test[備份].txt
。
2、步驟
接收用戶輸入的文件名。規(guī)劃備份文件名。備份文件寫入數(shù)據(jù)。
3、代碼實現(xiàn)
(1)接收用戶輸入目標文件名
old_name = input('請輸入您要備份的文件名:')
(2)規(guī)劃備份文件名
2.1 提取目標文件后綴。
2.2 組織備份的文件名,xx[備份]后綴。
# 2.1 提取文件后綴點的下標 index = old_name.rfind('.') # 2.2 組織新文件名 舊文件名 + [備份] + 后綴 new_name = old_name[:index] + '[備份]' + old_name[index:]
(3)備份文件寫入數(shù)據(jù)
3.1 打開源文件 和 備份文件。
3.2 將源文件數(shù)據(jù)寫入備份文件。
3.3 關(guān)閉文件。
# 3.1 打開文件 old_f = open(old_name, 'rb') new_f = open(new_name, 'wb') # 3.2 將源文件數(shù)據(jù)寫入備份文件 # 如果不確定目標文件大小,循環(huán)讀取寫入, # 當讀取出來的數(shù)據(jù)沒有了終止循環(huán) while True: # 每次在原文件中讀取的內(nèi)容 con = old_f.read(1024) # 表示讀取完成了 if len(con) == 0: # 終止讀取 break # 新文件寫入讀取的數(shù)據(jù) new_f.write(con) # 3.3 關(guān)閉文件 old_f.close() new_f.close()
(4)思考
如果用戶輸入.txt
,這是一個無效文件,程序如何更改才能限制只有有效的文件名才能備份?
答:添加條件判斷即可。
# 有文件名,才能提取后綴 # 這里無法取得后綴,拼接的時候沒有后綴的變量 # 就會報錯 if index > 0: postfix = old_name[index:]
(5)完整編碼
1)傳統(tǒng)實現(xiàn)
# 1. 用戶輸入目標文件 如:sound.txt.mp3 old_name = input('請輸入您要備份的文件名:') # 2. 規(guī)劃備份文件的名字 # 2.1 提取后綴 -- # 找到名字中的最右側(cè)的點才是后綴的點 # 在右側(cè)查找rfind()方法 # 獲取文件全名中后綴.的位置 index = old_name.rfind('.') # 4. 思考:有效文件才備份 .txt if index > 0: # 提取后綴,這里提取不到,后面拼接新文件名字的時候就會報錯 postfix = old_name[index:] # 2.2 組織新名字 = 原名字 + [備份] + 后綴 # 原名字就是字符串中的一部分子串 -- 切片[開始:結(jié)束:步長] # new_name = old_name[:index] + '[備份]' + old_name[index:] new_name = old_name[:index] + '[備份]' + postfix # 3. 備份文件寫入數(shù)據(jù)(數(shù)據(jù)和原文件一樣) # 3.1 打開 原文件 和 備份文件 old_f = open(old_name, 'rb') new_f = open(new_name, 'wb') # 3.2 原文件讀取,備份文件寫入 # 如果不確定目標文件大小,循環(huán)讀取寫入,當讀取出來的數(shù)據(jù)沒有了終止循環(huán) while True: # 每次在原文件中讀取的內(nèi)容 con = old_f.read(1024) # 表示讀取完成了 if len(con) == 0: # 終止讀取 break # 新文件寫入讀取的數(shù)據(jù) new_f.write(con) # 3.3 關(guān)閉文件 old_f.close() new_f.close()
2)實際工作實現(xiàn)
# 1. 用戶輸入目標文件 如:sound.txt.mp3 old_name = input('請輸入您要備份的文件名:') # 獲取文件全名中后綴.的位置 index = old_name.rfind('.') # 4.有效文件才備份 .txt if index > 0: postfix = old_name[index:] # 3.開始備份文件 # 打開原文件 with open(old_name , 'rb') as file_obj: # 組織新名字 = 原名字 + [備份] + 后綴 new_name = old_name[:index] + '[備份]' + postfix # 創(chuàng)建并打開新文件 with open(new_name, 'wb') as new_obj: # 定義每次讀取的大小 chunk = 1024 * 100 while True: # 從已有的對象中讀取數(shù)據(jù) content = file_obj.read(chunk) # 內(nèi)容讀取完畢,終止循環(huán) if not content: break # 將讀取到的數(shù)據(jù)寫入到新對象中 new_obj.write(content)
兩種方式實現(xiàn)的功能一樣。
4、再來一個小練習(xí)
需求:二進制文件讀?。▽崿F(xiàn)方式和上邊一樣)
# 讀取模式 # t 讀取文本文件(默認值) # b 讀取二進制文件 file_name = “hello.txt” with open(file_name , 'rb') as file_obj: # 讀取文本文件時,size是以字符為單位的 # 讀取二進制文件時,size是以字節(jié)為單位 # print(file_obj.read(100)) # 將讀取到的內(nèi)容寫出來 # 定義一個新的文件 new_name = 'aa.txt' with open(new_name , 'wb') as new_obj: # 定義每次讀取的大小 chunk = 1024 * 100 while True : # 從已有的對象中讀取數(shù)據(jù) content = file_obj.read(chunk) # 內(nèi)容讀取完畢,終止循環(huán) if not content : break # 將讀取到的數(shù)據(jù)寫入到新對象中 new_obj.write(content)
注意:純文本文件也可以使用二進制方法進行讀取操作。
總結(jié)
原文鏈接:https://www.cnblogs.com/liuyuelinfighting/p/15853186.html
相關(guān)推薦
- 2022-06-29 tomcat下部署jenkins的實現(xiàn)方法_Tomcat
- 2022-12-24 利用C語言實現(xiàn)頁面置換算法的詳細過程_C 語言
- 2023-07-31 element中el-input無法輸入
- 2022-08-15 LCP創(chuàng)建bond接口
- 2022-06-14 教你使用docker查看運行中的容器_docker
- 2022-04-07 C語言的線性表之順序表你了解嗎_C 語言
- 2022-03-03 實現(xiàn)不需要手動浮空瀏覽器緩存,程序可以獲取最新版本
- 2022-06-12 基于Docker搭建iServer集群_docker
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支