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" )
print a
print b
顯示:
c:\123\456\test
.txt
3,判斷乙個路徑( 目錄或檔案)是否存在
b = os.path.exists( "你要判斷的路徑")
返回值b: true 或 false
4,判斷乙個路徑是否檔案
b = os.path.isfile( "你要判斷的路徑")
返回值b: true 或 false
5,判斷乙個路徑是否目錄
b = os.path.isdir( "你要判斷的路徑")
返回值b: true 或 false
6,獲取某目錄中的檔案及子目錄的列表
l = os.listdir( "你要判斷的路徑")
l = os.listdir( "c:/" )
7.建立子目錄:
os.makedirs( "c:\\123\\456\\789")
8,刪除子目錄:
os.rmdir( path )
9,刪除檔案:
os.remove( filename )
10,檔案改名:
os.name( oldfilename, newfilename)
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.開啟檔案 如下 f open d test.txt w 說明 第乙個引數是檔名稱,包括路徑 第二個引數是開啟的模式mode r 唯讀 預設。如果檔案不存在,則丟擲錯誤 w 只寫 如果檔案 不存在,則自動建立檔案 a 附加到檔案末尾 r 讀寫 如果需要以二進位制方式開啟檔案,需要在mode後面加上...
python檔案操作
python中對檔案 資料夾 檔案操作函式 的操作需要涉及到 os模組和 shutil 模組。得到當前工作目錄,即當前python 指令碼工作的目錄路徑 os.getcwd 返回指定目錄下的所有檔案和目錄名 os.listdir 函式用來刪除乙個檔案 os.remove 刪除多個目錄 os.remo...