1. 程式人生 > >Windows system 在python檔案操作時的路徑表示方法

Windows system 在python檔案操作時的路徑表示方法




file_path =(r'i:\vacpy\ch10\pi_digits.txt') #將檔案路徑儲存在變數file_path中
with open (file_path) as file_object:
contents = file_object.read()
print(contents)

"""windows system 有時能夠正確地解讀檔案路徑中的斜杆。如果你使用的是windows system,且結果不符合
預期,請確保檔案路徑中使用的是反斜杆。另外,由於反斜杆在python中被視為轉義標記,為在windows中確保萬無一失
,應以原始字串的方式指定路徑,即在開頭的單引號前加上字母“r” """