Python檔案操作(2)

2021-10-07 04:40:08 字數 904 閱讀 4040

os.path.dirname(path) 返回path引數中的路徑名稱和字串

os.path.basename(path) 返回path引數中的檔名

os.path.splot(path) 返回引數的路徑名稱和檔名組成的字串元組

import os

totalsize =0

os.chdir("f:\\python")

for filename in os.listdir(os.getcwd()):

totalsize += os.path.getsize(filename)

print(totalsize)

os.rename("d:\\python\\hello.txt","d:\\python\\helloworld.txt")
shutil.copy(soure,destination) :複製檔案

shutil.copytree(source,destination) :複製整個資料夾,包括其中的檔案及子資料夾

import shutil

shutil.copytree("f:\\python","f:\\python1")

shutil.move(source,destination)

shutil.move("e:\\python\\data1.txt","e:\\python\\date2")
os.remove(path)/os.unlink(path):刪除引數path指定的檔案

os.rmdir(path):如前所述,os.rmdir()函式只能刪除空檔案更何況

shuil.retree(path):shutil.retree()函式刪除整個空資料夾,不可恢復

python檔案操作2

讀寫 定位 f.seek 偏移量,0,1,2 0,開頭,預設。1,當前位置。2,檔案末尾。f.tell 檢視當前指標的位置。f open a.txt rb print f.tell f.seek 2 2 print f.tell print f.read print f.tell f.close 讀...

python檔案基礎操作 2

對於檔案操作,python提供如下功能 唯讀形式讀檔案 def file r f open mydirtest 1.txt mode r encoding utf 8 print f.read f.close 寫入檔案,要先將1.txt的檔案內容清空之後,在進行寫入,不可讀 def file w f...

2 Python 簡單檔案操作

name sweet1 file1 open s.v name,w 不指定路徑則在絕對路徑生成,另外生成的檔名可以使用 替換 file2 open sweet2.v r file1中的 w 代表可對該檔案進行寫入操作,而 r 代表唯讀 file1.close 關閉檔案 file2.close 還可使...