我想替换这个: 路径 = r'C:/文件夹 有了这个: 路径 = r'变量
变量在其他地方定义。
不确定如何将变量名称放在 r' 之后
这应该有效:
path = r'%s' % variable
但是,如果您有一个带有反斜杠的字符串文字。您可以使用 String.raw 来转义它。
这类似于 Python 中的
r
前缀,或 C# 中字符串文字的 @
前缀。它用于获取模板文字的原始字符串形式
例如:
// Create a variable that uses a Windows
// path without escaping the backslashes:
const filePath = String.raw`C:\Development\profile\aboutme.html`;
console.log(`The file was uploaded from: ${filePath}`);
// Expected output: "The file was uploaded from: C:\Development\profile\aboutme.html"
如果你想将 r 添加到路径,那么这应该可以,但是你不能直接将 r 添加到路径变量,因为它是一种语法。如果你遇到 Unicode Escape Error ,这可以有效地使用由于路径中存在“\”而发生。
这是代码:-
raw_string path=path.encode('unicode_escape').decode()
这将像 r'path'
一样工作