Python中對於檔案的操作 增 刪 改

2021-10-12 08:33:16 字數 933 閱讀 6249

資料夾的建立

# 在當前目錄下建立資料夾

os.makedirs(

'./dict_phone/', exist_ok=true)

# 在上一級目錄下建立資料夾

os.makedirs(

'../dict_phone/', exist_ok=true)

檔案的複製
import shutil

# 引數 舊路徑:新路徑

shutil.copy(

'../dict_phone/test.json','../dict_phone_move/'

)# 將上級目錄下的dict_phone資料夾下的test.json檔案複製到上級目錄下的dict_phone_move資料夾下

檔案的移動(剪下)
import shutil

# 引數 舊路徑:新路徑

shutil.move(

'../dict_phone/test.json','../dict_phone_move/'

)# 將上級目錄下的dict_phone資料夾下的test.json檔案移動(剪下)到上級目錄下的dict_phone_move資料夾下

檔案的重新命名
import os

# 修改檔案名字 引數 舊檔案 新檔案

os.rename(

'test.txt','retest.txt'

)# 將同級目錄下的test.txt檔案重新命名為retest.txt

檔案的刪除
import os

# 引數 舊路徑:新路徑

os.remove(

'../dict_phone/test.json'

)# 將上級目錄下的dict_phone資料夾下的test.json檔案給刪除

參考:

Python實現對於xml檔案中資料的增刪改查

coding utf 8 import ete.cryptolib import ete.initete from xml.etree import elementtree as et 1 載入檔案 tree et.parse carddata.xml 2 獲得根結點 root tree.getro...

python中對於棧的操作

class stack object 棧的資料結構,棧就是容器,先進後出 def init self self.mylist def push self,item 壓棧 往棧頂放入元素,假設選擇列表的尾部作為棧頂 def pop self 從棧頂彈出乙個元素 return self.mylist.p...

檔案操作 增改查

修改檔案的兩種方式 1.第一種方法 在原檔案中修改 先開啟檔案,把檔案內容讀出來賦值給乙個變數,關閉檔案,重新開啟檔案,把檔案內容寫到檔案中 with open r f r encoding utf 8 as f data f.read print data print type data with...