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

标签 python-3.x file

我正在尝试将多行内容写入 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/

相关文章:

Java:无法加载属性文件。为什么?

c++ - 从文件输入和输出

python: pycodestyle (ex pep8) vs pylint 严格性

python - 在 Python3 ubuntu 上安装 tkinter

python - 为什么我收到错误 : No module named 'email.MIMEMultipart' ?

Java,扩展File以获得更多属性

python - 用Python读取文件,出现奇怪的错误

c - 在 C 中确定文件类型

python - 文件内容没有预期的那么长

python - 删除单引号和分隔括号