如何使用Python在文件路径中包含变量

问题描述 投票:0回答:1

我有文件路径

say='5.0'
output_path=r'C:\User\Projects\'+ say

我收到错误消息。

SyntaxError: EOL while scanning string literal

更新!!!我也尝试根据您的建议执行此操作,但效果不佳

output_path=r'C:\User\Projects\\'+ say

我将字符串作为

'C:\\User\\Projects\\\\\5.0

我希望output_path基本上是

C:\User\Projects\5.0
python python-3.x windows filepath
1个回答
-2
投票

output_path = r'C:\ User \ Projects \'+说从字符串中取出“ r”,并且您可能可以使用字节(output_path,“ utf-8”),因此您基本上可以在字符串之前执行“ r”的操作顺便说一句,是“ r”在字符串之前吗?因为如果没有的话,只需删除它,它将可以正常工作:D

© www.soinside.com 2019 - 2024. All rights reserved.