错误原因是,字符串以 \ 结尾 或者字符串缺少引号。

写代码拼接windows 路径出现这个错误, 查资料才知道 python中字符串不能以 \ 结尾

我的代码如下

import os
dirname = "test" path = r'C:\Users\panda\Desktop\新建文件夹\' + dirname

运行则报错

  File "test.py", line 3
path = r'C:\Users\panda\Desktop\新建文件夹\' + dirname
^
SyntaxError: EOL while scanning string literal

那么如何解决呢

方法一 : 使用 os.path.join

path = os.path.join(r'C:\Users\panda\Desktop\新建文件夹', dirname)

方法二:路径的反斜杠使用转义 而不用 r

path = 'C:\\Users\\panda\\Desktop\\新建文件夹\\' + dirname

方法三:格式化字符串

dirname="test"
path = r'C:\Users\panda\Desktop\新建文件夹\%s' % (dirname) # 第一种格式化方法
#从 python 2.6 开始
path = r'C:\Users\panda\Desktop\新建文件夹\{}'.format(dirname) # 第二种格式化方法

方法四: string interpolation (字符串内插)

从python 3.6 开始 支持string interpolation

# python 3.6 开始 支持string interpolation
dirname = "test"
path3 = rf'C:\Users\panda\Desktop\新建文件夹\{dirname}'

参考: https://docs.python.org/3/whatsnew/3.6.html#whatsnew36-pep498

为何 字符串不能 以 \ (反斜杠) 结束呢

因为 反斜杠有别的用处。 python中一个完整的字符串太长的时候,一行写不下想换行,但又要维持它是一个字符串的时候 可以用 反斜杠来换行,所以反斜杠后面不能立即接上字符串结束的引号。

下面用 REPL演示

参考资料:

SyntaxError: EOL while scanning string literal的解决 - CSDN博客

python: SyntaxError: EOL while scanning string literal - Stack Overflow

python SyntaxError: EOL while scanning string literal的更多相关文章

  1. SyntaxError: EOL while scanning string literal的解决

    2281 python中字符串的最后一个字符是斜杠会导致出错:SyntaxError: EOL while scanning string literal [背景] Python 2.7.2 中想要通 ...

  2. python ( EOL while scanning string literal)

    python错误: EOL while scanning string literal: 这个异常造成的原因是字符串,引号没有成对出现 参考:http://www.jb51.net/article/6 ...

  3. SyntaxError: EOL while scanning string literal

    在Python 中,这个提示,一般是因为特殊字符引起的,比如换行符,比如 \ 等. 下面有几个示例: 1. 换行符 # 源错误代码 get_tabs="select b.owner,b.ta ...

  4. python接口自动化,从excel取param的内容太多,使用eval转换报错'EOL while scanning string literal

    背景: 做接口自动化时,有个接口的参数内容很多,可以从excel中读取出来,但是在eval()进行转化时,就报错"'EOL while scanning string literal&quo ...

  5. [Error]EOL while scanning string literal

    有一个经常性的工作项目.需要一天的一些表数据到外部接口,但最近总是异常.今天检查的原因. 第一本地和测试环境中测试程序是没有问题,有网络环境只会在日志中抛出一个异常.产生主要的例外是推定异常数据. , ...

  6. 转 [Error]EOL while scanning string literal

    https://blog.csdn.net/orangleliu/article/details/38943749 项目中有个定时任务,每天取到一些表数据传到一个外部接口,但是最近总是有异常,今天查了 ...

  7. 后台传给前端字符串为null或解析JSON字符错误——SyntaxError: JSON.parse: unterminated string literal at line 1 column 9018638 of the JSON data

    第一种情况: 第二种情况: 首先看看你的JSONObject或JSONArray的引用有没有Getter()和Setter()方法,这个必须要加上 问题:两张表双向多对一.一对多时.响应给后台使,出现 ...

  8. 使用MySql数据库, 浏览器接收返回数据报错SyntaxError: unterminated string literal

    用php写了一个很简单的页面, 用来记录常用的oracle的关键字和各种函数, 以后用起来查一下方便, 原来是用java写了一个web项目, 但是用起来太麻烦, 真的不如php方便, 然后就把整理的内 ...

  9. SyntaxError: JSON.parse: bad control character in string literal at line 1 column 16 of the JSON data

    JSON.parse转化Json字符串时出现:SyntaxError: JSON.parse: bad control character in string literal at line 1 co ...

随机推荐

  1. python pandas使用一些协程

    import pandas as pd def coroutine(func): """装饰器:向前执行到第一个`yield`表达式,预激`func`"&quo ...

  2. 关于ASP.NET MVC+Repository+Service架构的一些思考

    看了一些ASP.NET MVC开源项目后的一些想法,关于ASP.NET MVC+Repository+Service架构的一些思考 最近在学习ASP.NET MVC 2.0的一些开源项目,发现这些项目 ...

  3. django notes 一:开篇

    公司 web 框架用的是 django, 以前没用过,打算这两周好好看看. 边学习边整理一下笔记,加深理解. 好像谁说过初学者更适合写入门级的教程,我觉得有一定道理. 高手写的教程有一定深度,不会写入 ...

  4. RESTful简单介绍

    1 什么是restful Restful就是一个资源定位及资源操作的风格.不是标准也不是协议,只是一种风格.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制. 资源:互联网所有的事物都 ...

  5. validate表单校验插件笔记

    1validation知识点 1 validation基础 validation插件下载http://bassistance.de/jquery-plugins/jquery-plugin-valid ...

  6. 手写css按钮组

    css: .lf{float:left} .btn{ width:60px; height:24px; color:#fff; border-radius:4px; cursor:pointer; b ...

  7. python 对象/变量&赋值的几点思考

    python 对象/变量 对象 Every object has an identity, a type and a value. An object's identity never changes ...

  8. html中文乱码(解决办法)

    在head标签中加上以下代码即可: <head>     <meta http-equiv="Content-Type" content="text/h ...

  9. [转]OLAP的12条准则

    OLAP的12条准则 Multidimensional conceptual view OLAP模型必须提供多维概念视图 User-analysts would view an enterprise ...

  10. C#语言-07.文件操作

    a. 文件操作:适用于相对简单的数据保存 i. 读写文件的步骤: . 创建文件流 . 创建读写器 . 读写文件 . 关闭读写器 . 关闭文件流 ii. FileStream(文件流),它主要用于读写文 ...