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')

  

相关文章:

  • 2022-12-23
  • 2021-09-21
  • 2021-05-11
猜你喜欢
  • 2022-01-03
  • 2022-02-18
  • 2022-12-23
  • 2022-12-23
  • 2021-08-07
  • 2021-09-23
  • 2021-05-19
相关资源
相似解决方案