Python:阻止字符串上的%s替换

Par*_*and 3 python

有没有办法标记一个字符串,以便它不会触发python的%字符串替换?

特别是,我正在尝试使用python sftp模块下载名称包含%的文件.sftp模块尝试使用日志记录模块记录文件的名称:

self.logger.log(level, msg, *args)
Run Code Online (Sandbox Code Playgroud)

logging因为%符号而抱怨字符串格式:

  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 328, in getMessage
    msg = msg % self.args
TypeError: not enough arguments for format string
Run Code Online (Sandbox Code Playgroud)

所以:既然我不控制文件名和sftp模块,我可以将文件名标记为不参与字符串替换吗?

Kur*_*ler 5

mystring.replace('%', '%%')可能适合您的需求.%%逃避了%.