如果是在调试终端执行脚本,那么current_dir 往往不是脚本文件本身所在的当前目录,通过获取脚本文件所在目录就可以了。
# 获取当前目录的路径 current_dir = os.getcwd() # 获取脚本文件所在目录的路径 script_dir = os.path.dirname(os.path.realpath(__file__)) # 在当前目录下创建文件 file_path = os.path.join(script_dir, 'tips.txt') with open(file_path, 'w') as f: f.write(tips_msg) print('File created successfully.')
失落的羊4 个月前
研究插件:挂载点研究、文件读写研究、API读取数据、设置、前台显示
失落的羊5 个月前
今日申请十年之约博客成员!
失落的羊5 个月前
启用新的访问统计.
失落的羊5 个月前
重新整理长篇连载栏目
失落的羊6 个月前
构思公众号文章迁移计划