我正在尝试使用Python 2.7中的路径
这是我在主要课程中要做的事情:
program = MyProgram()
program.doSomething('C:\Eclipse\workspace\MyProgram\files\12345678_Testing1_ABCD005_Static_2214_File12.txt')
在函数内部,doSomething(filePath)
字符串看起来像这样:
所以有一种特殊的字符加上一些字符被完全删除。什么可能导致这个问题?
发布于 2019-07-17 15:43:50
这可能是你的情况有点过分,你可以os.path.join
用来建立路径。有了这个,你有两个好处:
如上所述,这对您的代码来说可能有点过分:
import os.path
program = MyProgram()
my_path = os.path.join('C:',
'Eclipse',
'workspace',
'MyProgram',
'files',
'12345678_Testing1_ABCD005_Static_2214_File12.txt')
program.doSomething(my_path)
发布于 2019-07-17 16:32:10
\
是Python中的转义字符。According to docs,您已经创建了字符串\f ASCII Formfeed (FF)
character.
字符串文字可以用匹配的单引号括起来(
'
) or double quotes ("
)。它们也可以包含在三个单引号或双引号的匹配组中(通常称为三引号)。The backslash (**\
**) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.
Either use double slashes (\\
- to escape escape character) or use raw string literals (r"some\path"
).
字符串文字可以选择性地以字母‘r’或‘R’作为前缀;这类字符串称为原始字符串,并使用不同的规则解释反斜杠转义序列。
https://stackoverflow.com/questions/-100007077
复制相似问题