[問題] Python 檔案目錄與讀取

看板Python作者 (好檸檬)時間5年前 (2019/08/12 13:56), 編輯推噓1(103)
留言4則, 3人參與, 5年前最新討論串1/1
希望這篇不要被噓爆,我應該不是伸手牌 我真的是初學者,目前只會按造書中範例學習 我是買碁峯的書 (初學特訓班) 其中第四章 檔案處理與SQLite資料庫 對檔案的路徑,實在看不懂,google後,一些相對路徑絕對路徑,越看越頭大 完全昏頭了,所以來這提問 書中4.1 檔案與目錄管理 書中附的原始碼 remove() import os file = "myFile.txt" if os.path.exists(file): os.remove(file) else: print(file + "檔案未建立!") ============================================= mkdir()方法 import os dir = "myDir" if not os.path.exists(dir): os.mkdir(dir) else: print(dir + "已經建立!") ============================================= 想詢問 1. file = "myFile.txt" os.mkdir(dir) myFile.txt是在哪個目錄? 搞不清楚 我的想法是,需要宣告/指定路徑,比如 D:\python 來這請教版友,感謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 203.64.247.2 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1565589377.A.6B7.html

08/12 14:43, 5年前 , 1F
沒有指定路徑就是跟你的script在同一個層
08/12 14:43, 1F

08/12 16:14, 5年前 , 2F
檔案部份,用絕對路徑比較不會錯
08/12 16:14, 2F

08/12 17:34, 5年前 , 3F
可以參考 https://tinyurl.com/y24g6t6q。用IDLE互動視
08/12 17:34, 3F

08/12 17:34, 5年前 , 4F
窗試一下就知道了
08/12 17:34, 4F
文章代碼(AID): #1TKF-1Qt (Python)
文章代碼(AID): #1TKF-1Qt (Python)