python 檔案操作

2021-09-23 16:41:51 字數 395 閱讀 4225

python檔案操作

#判斷檔案是否存在

os.path.exists(src_dir)

#檔案拷貝

open(des_file_name, "wb").write(open(src_file_name, "rb").read())

#檔案重新命名

os.rename(old_name, new_name)

#列出指定目錄下的所有檔名稱

os.listdir(path_name)

#刪除單個檔案

os.remove()

# 刪除空檔案

os.rmdir()

# 刪除指定目錄以及該目錄下所有的檔案

shutil.rmtree(src_dir)

python 檔案操作

簡明 python 教程 中的例子,python 執行出錯,用open代替file 可以執行。poem programming is fun when the work is done if you wanna make your work also fun use python f open e ...

python檔案操作

1,將乙個路徑名分解為目錄名和檔名兩部分 a,b os.path.split c 123 456 test.txt print a print b 顯示 c 123 456 test.txt 2,分解檔名的副檔名 a,b os.path.splitext c 123 456 test.txt pri...

Python 檔案操作

1.開啟檔案 如下 f open d test.txt w 說明 第乙個引數是檔名稱,包括路徑 第二個引數是開啟的模式mode r 唯讀 預設。如果檔案不存在,則丟擲錯誤 w 只寫 如果檔案 不存在,則自動建立檔案 a 附加到檔案末尾 r 讀寫 如果需要以二進位制方式開啟檔案,需要在mode後面加上...