大家好,我怎樣才能讓這件事成真?我已經存在 .txt 檔案,但結果總是 False。
ID = input("Enter the name of your .txt file: ") ".txt" "'"
IDS = "'" ID
file_exists = os.path.exists(IDS)
print(file_exists)
print(IDS)
uj5u.com熱心網友回復:
去掉'前后檔案名:
ID = input("Enter the name of your .txt file: ") ".txt"
file_exists = os.path.exists(ID)
print(file_exists)
print(ID)
例如,如果你有foo.txt并且你放入foo你的程式,那么你當前的代碼將尋找一個名為 的檔案'foo.txt',而不是 foo.txt.
uj5u.com熱心網友回復:
嘗試這個:
#You may want to use str.strip() to get rid of unnecessary whitespaces
ID = input("Enter the name of your .txt file: ").strip() ".txt"
file_exists = os.path.exists(ID)
print(f'{ID} exists') if file_exists else print(f'{ID} file does not exist')
uj5u.com熱心網友回復:
洗掉引號,您在字串的開頭和結尾添加單引號。您的代碼應如下所示:
ID = input("Enter the name of your .txt file: ") ".txt"
file_exists = os.path.exists(ID)
print(file_exists)
print(IDS)
編程語言中的引號表示什么是字串,當您將其輸出到終端(列印)時,它們會被解釋器忽略
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/337994.html
上一篇:基于列的Numpy過濾矩陣
