簡體   English   中英

Python 找不到我的 txt 文件在哪里,我如何鏈接它或我需要把文件放在哪里?

[英]Python can't find where my txt file is, how can i link it or where do i need to put the file?

Python 找不到 my.txt 文件在哪里,我怎樣才能找到它的正確路徑或者我需要把文件放在哪里?

我嘗試了以下方法,但出現錯誤:

with open("C:\Users\raynaud\Downloads\notlar.txt","r",encoding="utf-8") as file:

with open("dosya.txt","r",encoding= "utf-8") as file:

FileNotFoundError: [Errno 2] No such file or directory: 'dosya.txt'

如果不使用絕對路徑,則必須將文件放在與腳本相同的目錄中。 在絕對路徑上,您不應使用 \ 像"C:\Users\UserName\" ,因為 \ 是 Python (以及更多語言)中的轉義字符。 您必須像這樣使用它: "C:\\Users\\UserName\\"

你檢查過你的當前目錄嗎? 它可能指向一個意想不到的地方。 嘗試:

import os
os.getcwd()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM