gpt4 book ai didi

python-3.x - 如何在 Python 中将文件路径写入 txt 文件

转载 作者:行者123 更新时间:2023-12-04 17:24:56 24 4
gpt4 key购买 nike

我正在尝试将多行内容写入 Python 中的文本文件

with open('output.txt', 'w') as outfile:
outfile.write("open\n" +
"c:\files\file.scr"+
"close"
)

文件路径写错了,

   open
c: iles\drawing.dwgclose

有人可以建议如何写文件路径吗?

最佳答案

你可以通过在前面添加r来使字符串raw:

with open('output.txt', 'w') as outfile:
outfile.write("open\n" +
r"c:\files\file.scr"+
"close"
)

关于python-3.x - 如何在 Python 中将文件路径写入 txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64074134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com