昨晚搞鼓了一下python的open()打開文件 代碼如下
def main():
infile =open("C:\Users\Spirit\Desktop\bc.txt",'r')
data = infile.read()
print(data)
main()
然而結果總報錯invaild argument 或者cant found such file ***
查找問題后 發現是由於python中的 ‘\’ 是轉義符號,要想輸出\ 的辦法有兩種
1 、在\后再加\ 就是\\ 的形式
把第二行改為infile =open("C:\\Users\\Spirit\\Desktop\\bc.txt",'r') 即可
2、在路徑前加個 r ,意思是按原始字符處理 。
eg: infile =open(r"C:\Users\Spirit\Desktop\bc.txt",'r')
好的 文件就可以讀取了!
