[英]What is the correct way to write a file path, to access a document in a folder on your computer
我不斷收到錯誤,該文件不存在。 這是我第一次這樣做,在谷歌上找不到足夠的答案。
該文件存儲在名稱為“maths”的文件夾中,文檔名稱為“Data.txt”
我為閱讀內容而編寫的代碼是:
def read(data):
try:
count=0
INFILE=open("maths\\Data.txt","r")
for line in INFILE:
rawdata.append(line.rstrip())
count+=1
INFILE.close()
return count
except:
print("File could not be found")
return(count)
exit()
一種可靠的方法是列出文本文件的整個路徑。
假設文件夾在桌面,你電腦的用戶名是 User1:
def read(data):
try:
count=0
INFILE=open("C:\\Users\\User1\\Usermaths\\Data.txt","r")
for line in INFILE:
rawdata.append(line.rstrip())
count+=1
INFILE.close()
return count
except Exception as e:
print(e)
return(count)
exit()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.