import os
file_path =
"e:\python_linux\c_file.txt"
dir_path =
"e:\python_linux"
print
("刪除前,當前路徑下的檔案:"
,os.listdir(dir_path)
)os.remove(file_path)
print
("刪除c_file.txt後,當前路徑下的檔案:"
,os.listdir(dir_path)
)
結果:
刪除前,當前路徑下的檔案: [
'c_file.txt'
,'file.txt'
,'file_operate'
,'s004_string_judge.py'
,'s005_string_find_fuction.py'
,'s006_string_reguler_express.py'
,'write_function.txt'
,'w_file.txt'
]刪除c_file.txt後,當前路徑下的檔案: [
'file.txt'
,'file_operate'
,'s004_string_judge.py'
,'s005_string_find_fuction.py'
,'s006_string_reguler_express.py'
,'write_function.txt'
,'w_file.txt'
]>>
>
print
("刪除前test_delete_dir,當前路徑下的檔案:"
,os.listdir(dir_path)
)os.rmdir(delet_dir_path)
time.sleep(1)
print
("刪除後test_delete_dir,當前路徑下的檔案:"
,os.listdir(dir_path)
)
結果
刪除前test_delete_dir,當前路徑下的檔案: [
'file.txt'
,'file_operate'
,'s004_string_judge.py'
,'s005_string_find_fuction.py'
,'s006_string_reguler_express.py'
,'test_delete_dir'
,'write_function.txt'
,'w_file.txt'
]刪除後test_delete_dir,當前路徑下的檔案: [
'file.txt'
,'file_operate'
,'s004_string_judge.py'
,'s005_string_find_fuction.py'
,'s006_string_reguler_express.py'
,'write_function.txt'
,'w_file.txt'
]
print
("未建立test_mkdir資料夾前,當前路徑下的檔案:"
,os.listdir(dir_path)
)os.mkdir(os.path.join(dir_path,
'test_mkdir'))
print
("建立test_mkdir資料夾後,當前路徑下的檔案:"
,os.listdir(dir_path)
)
結果
未建立test_mkdir資料夾前,當前路徑下的檔案: [
'file.txt'
,'file_operate'
,'s004_string_judge.py'
,'s005_string_find_fuction.py'
,'s006_string_reguler_express.py'
,'write_function.txt'
,'w_file.txt'
]建立test_mkdir資料夾後,當前路徑下的檔案: [
'file.txt'
,'file_operate'
,'s004_string_judge.py'
,'s005_string_find_fuction.py'
,'s006_string_reguler_express.py'
,'test_mkdir'
,'write_function.txt'
,'w_file.txt'
]
print
("未重新命名test_mkdir資料夾前,當前路徑下的檔案:"
,os.listdir(dir_path)
)os.rename(os.path.join(dir_path,
'test_mkdir'
),os.path.join(dir_path,
'haha_dir'))
print
("重新命名test_mkdir為haha_dir資料夾後,當前路徑下的檔案:"
,os.listdir(dir_path)
)
結果
未重新命名test_mkdir資料夾前,當前路徑下的檔案: [
'file.txt'
,'file_operate'
,'s004_string_judge.py'
,'s005_string_find_fuction.py'
,'s006_string_reguler_express.py'
,'test_mkdir'
,'write_function.txt'
,'w_file.txt'
]重新命名test_mkdir為haha_dir資料夾後,當前路徑下的檔案: [
'file.txt'
,'file_operate'
,'haha_dir'
,'s004_string_judge.py'
,'s005_string_find_fuction.py'
,'s006_string_reguler_express.py'
,'write_function.txt'
,'w_file.txt'
]
python檔案管理 python檔案管理
檔案io常見操作 open 開啟 read 讀取 write 寫入 close 關閉 readline 行讀取 readlines 多行讀取 seek 檔案指標操作 tell 指標位置 開啟操作 open file,mode r buffering 1,encoding none,erroes no...
python 檔案管理
r 只能讀 不能寫 讀取檔案不存在,是會報錯 r 可以執行讀寫操作,但寫的內容會覆蓋 檔案不存在,報錯 w 只能寫,不能讀 會清空檔案內容 檔案不存在,會新建檔案 w rw 檔案不存在,不報錯 會清空檔案內容 a 只能寫 不會清空問檔案內容 檔案不存在,會新建檔案 a rw 檔案不存在,不報錯 不會...
python檔案管理與模組
輸入 raw input input 程式處理 輸出 print 開啟檔案 f open filename 處理檔案 f.read 可以讀取檔案內容 f.write hello 不能往檔案中寫入,因為預設檔案以r的模式開啟 關閉並儲存檔案 f.close 若檔案不存在,直接報錯 檔案只能讀取,不能寫...