# 1.建立檔案
# 1.1 使用常規方式--建立開啟並關閉檔案
fp = open("file_name.txt")
fp.close()
# 1.2 建立檔案--推薦使用上下文管理器
with open("123\\file_name.txt", 'a+') as fp:
pass
# 1.3 linux可以使用os.mknod(),
# 因為在windows上沒有node這個概念, # attributeerror: module 'os' has no attribute 'mknod'
os.mknod('cs.txt','r')
# 2. 刪除檔案
os.remove('123')
# 3. 建立資料夾
os.mkdir('t1234')
# 4. 刪除資料夾
# 4.1 刪除空資料夾
os.removedirs('t1234')
# 4.2 刪除非空資料夾 -- 使用相應的模組
import shutil
shutil.rmtree('t1234')
# 4.3 刪除非空資料夾 -- 呼叫系統命令
import platform
import os
path = 't1234' # 指定要刪除的非空資料夾
if 'windows' == platform.system():
os.system('rd /s /q {}'.format(path))
elif 'linux' == platform.system():
os.system('rm -rf {}'.format(path))
python檔案及資料夾操作
一 python中對檔案 資料夾操作時經常用到的os模組和shutil模組常用方法。1.得到當前工作目錄,即當前python指令碼工作的目錄路徑 os.getcwd 2.返回指定目錄下的所有檔案和目錄名 os.listdir 3.函式用來刪除乙個檔案 os.remove 4.刪除多個目錄 os.re...
python 檔案及資料夾操作
需要importos 表示當前目錄 當前目錄的父目錄 s os.getcwd 得到當前工作目錄,即當前python指令碼工作的目錄路徑 d ss py os.chdir r d ss 改變工作目錄 os.curdir 表示當前目錄 s os.pardir 表示當前目錄的父目錄 os.chdir os...
Python 檔案及資料夾操作 os
import os os.mkdir 新資料夾 注意如果要建立的資料夾已經存在的話會報錯的 import os if not os.path.exists 新資料夾 os.mkdir 新資料夾 import os os.makedirs 第一層資料夾 第二層資料夾 第三層資料夾 shutil.cop...