1.相對路徑
windows '.\'表示當前路徑
with open(r'.\db\123.txt','w',encoding='utf-8') as f:
f.write('abc')
2.絕對路徑
2.1 直接加死絕對路徑
with open('c:\db\123.txt','w',encoding='utf-8') as f:
f.write('abc')
2.2 動態絕對路徑
import os,sys
project_path = os.path.dirname(os.path.abspath(__file__)) # 獲取當前文件路徑的上一級目錄
file_path = project_path+r'\db\123.txt' # 拼接路徑字符串
with open(file_path,'w',encoding='utf-8') as f:
f.write('abc')
