导入其他py文件时提示“unicode error”

来源:互联网 发布:red5源码 教程 编辑:程序博客网 时间:2024/06/14 10:42

1、import  sys

     sys.path.append("D:\\Users\\hello.py")

2、import sys

     sys.path.insert(0,"D:\\Users\\hello.py")

若为:sys.path.append("D:\Users\hello.py"),提示错误:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

因为一个反斜杠则会进行转义

3、line = f.readline()  #逐行读取

      line = f.read()   #读取全部

      line = f,read(size)    #读取一定的size

 也可用一个while循环,每次读取一行,直到读完

while line:

     print (line)

     line = f.readline()

f.close()

4、for line in open("amy.txt"):

           print (line)    #此方法可自动关闭文件


0 0