Python_报错:SyntaxError: EOL while scanning string literal

Python_报错:SyntaxError: EOL while scanning string literal

原因:python中,目录操作时,字符串的最后一个字符是斜杠,会导致出错,去掉\即可

上代码

>>> import os
>>> os.chdir(r"e:\")#字符串的最后一个字符是斜杠,会导致出错
  File "<stdin>", line 1
    os.chdir(r"e:\")
                   ^
SyntaxError: EOL while scanning string literal

解决方法:去掉最后的\即可

>>> import os
>>> os.chdir(r"e:")
>>>

原文地址:https://www.cnblogs.com/rychh/p/9744896.html

时间: 2024-08-26 01:00:32

Python_报错:SyntaxError: EOL while scanning string literal的相关文章

[Error]EOL while scanning string literal

有一个经常性的工作项目.需要一天的一些表数据到外部接口,但最近总是异常.今天检查的原因. 第一本地和测试环境中测试程序是没有问题,有网络环境只会在日志中抛出一个异常.产生主要的例外是推定异常数据. ,由不得而知,于是添加了程序的输出日志和数据打印,通过几次的执行定位了错误发生的函数和数据记录. 异常是这种: EOL while scanning string literal: <string>, line 1, pos 7 google一下.这个异常造成的原因是字符串,引號没有成对出现,也就是

Python_报错:SyntaxError: (unicode error) &#39;unicodeescape&#39; codec can&#39;t decode bytes in position 2-3: truncated \UXXXXXXXX escape

Python运行后,报错:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape 原因:window 读取文件可以用\,但是在字符串中\是被当作转义字符来使用,经过转义之后可能就找不到路径的资源了,例如\t会转义为tab键 上代码: >>> def func1(path_name): ... import os ..

Python_报错:TypeError: file must have &#39;read&#39; and &#39;readline&#39; attributes

Python 报错:TypeError: file must have 'read' and 'readline' attributes 在运行序列化(pickle)相关功能时报错:TypeError: file must have 'read' and 'readline' attributes 上代码: >>> fp = open("a.txt","r+") >>> import pickle >>> pic

前端控制台 JavaScript函数报错 SyntaxError: expected expression, got &#39;;&#39; SyntaxError: expected expression, got &#39;if&#39;

在火狐浏览器下调试时, 页面报错SyntaxError: expected expression, got ';'或者SyntaxError: expected expression, got 'if'等 其实就是js部分某个变量没有赋值到,例如: // 这种echo在前端输出的是空 var test = <?php echo 1 > 0 ? true : false ?>; 在前端js显示的是: var test = ; 注意: 在前端 test = ; 这样的变量没有赋值到就会报错,

python三元运算符—报错“SyntaxError: can&#39;t assign to conditional expression”

运行代码: a=1 b=1 x=1 if a==b else x=0 print(x) 提示错误: File "test.py", line 3 x=a if a==b else x=0 ^ SyntaxError: can't assign to conditional expression expression是表达式,就是加减乘除等各种运算符号连接起来的式子(statement是语句,如if语句,while,复制语句等): 三目运算中表达式只能作为左值 修改后: a=1 b=1

spring Mongodb查询索引报错 java.lang.NumberFormatException: empty String

笔者在mongo3.4/4.0环境下使用以下命令创建索引, db.getCollection("xxx.com").createIndex({ mobile: "" }, { name: "mobile_mac" }) 执行以下demo查询索引 mongoTemplate.indexOps(collection).getIndexInfo(); #笔者正式环境不是这么写的,这里方便大家理解,使用语法糖方式的代码 ;p 每次走到xxx.com表的时

Python_报错:TypeError: Tuple or struct_time argument required

报错:TypeError: Tuple or struct_time argument required 原因:时间戳和格式化处理时间不能直接使用会报错 上代码: import time time3 = time.asctime(time.time())# print ("本地时间为 :",time3) >>> import time >>> time3 = time.asctime(time.time())# Traceback (most rec

Python_报错:SyntaxError: unexpected character after line continuation character

原因:写入的文件内容不正确,应处理为字符串 >>> import os >>> os.makedirs(time_year+"\\"+time_month+"\\"+time_day)#其中的time_year.time_month.time_day都是有赋值的变量 >>> os.chdir(time_year\time_month\time_day)#问题出在这里,写法不对 File "<std

解决这个报错SyntaxError: Non-ASCII character

在文件头部添加这一句即可# -*- coding: utf-8 -* SyntaxError: Non-ASCII character '\xe5' in file ex16.py on line 1, ng declared; see http://python.org/dev/peps/pep-0263/ for details 意思是在文件中存在非ASCII字符(中文): 在头部添加 # -*- coding: utf-8 -*- 或 # -*- coding: cp936 -*-