我正在努力将文件从一个位置复制到另一个位置。使用shutil.copy
很容易。一切都很好,直到一个特殊的来源"来了。
文件的路径如下:
c:\install\NewCustomer\fire.exe
现在每当我执行此操作时,都会抛出错误:
'unicodeescape' codec can't decode bytes in position 975: malformed \N character escape
at IronPython.Runtime.LiteralParser.ParseString (System.Char[] text, Int32 start, Int32 length, Boolean isRaw, Boolean isUni, Boolean normalizeLineEndings) [0x00000] in <filename unknown>:0
at IronPython.Compiler.Tokenizer.MakeStringToken (Char quote, Boolean isRaw, Boolean isUnicode, Boolean isBytes, Boolean isTriple, Int32 start, Int32 length) [0x00000] in <filename unknown>:0
at IronPython.Compiler.Tokenizer.ContinueString (Char quote, Boolean isRaw, Boolean isUnicode, Boolean isBytes, Boolean isTriple, Int32 startAdd) [0x00000] in <filename unknown>:0
at IronPython.Compiler.Tokenizer.ReadString (Char quote, Boolean isRaw, Boolean isUni, Boolean isBytes) [0x00000] in <filename unknown>:0
at IronPython.Compiler.Tokenizer.Next () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Tokenizer.GetNextToken () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.FetchLookahead () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.NextToken () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.MaybeEat (TokenKind kind) [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.FinishAssignments (IronPython.Compiler.Ast.Expression right) [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseExprStmt () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseSmallStmt () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseSimpleStmt () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseStmt () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseSuite () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseTryStatement () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseStmt () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseSuite () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseClassOrFuncBody () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseFuncDef () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseStmt () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseSuite () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseClassOrFuncBody () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseClassDef () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseStmt () [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseFileWorker (Boolean makeModule, Boolean returnValue) [0x00000] in <filename unknown>:0
at IronPython.Compiler.Parser.ParseFile (Boolean makeModule, Boolean returnValue) [0x00000] in <filename unknown>:0
at IronPython.Runtime.PythonContext.ParseAndBindAst (Microsoft.Scripting.Runtime.CompilerContext context) [0x00000] in <filename unknown>:0
at IronPython.Runtime.PythonContext.CompilePythonCode (Microsoft.Scripting.SourceUnit sourceUnit, Microsoft.Scripting.CompilerOptions options, Microsoft.Scripting.ErrorSink errorSink) [0x00000]
这是因为特殊性格&#34; \ N&#34;。我知道如果我编写以下代码,它将起作用:
r"c:\install\NewCustomer\fire.exe"
但问题是源路径是动态生成的,因此在运行时我不知道是否必须处理这个特殊字符。有没有办法处理这种情况?
答案 0 :(得分:3)
为什么要为路径生成Python代码?将路径生成为文本文件,然后将其读入。
\n
并不是唯一可能导致问题的特殊字符 - 您可能必须始终使用原始字符串。
答案 1 :(得分:-2)
yourvariable.strip()
如果您在调用变量时运行该变量,则应删除换行符。