1. 程式人生 > >匯入其他py檔案時提示“unicode error”

匯入其他py檔案時提示“unicode error”

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)    #此方法可自動關閉檔案