在用戶輸入上,我試圖驗證他們輸入了合適的文件擴展名。例如:檢查有效的文件擴展名
file1 = input('Enter the file name ending in ".txt": ')
我想驗證他們是否輸入了'.txt'。現在我正在做非常基本的檢查。但是,下面的檢查並沒有真正驗證文件是否以.txt結尾。
if '.txt' not in file_name:
print('Include file extension (example): ".txt"')
所以我認爲它只驗證'txt'字符存在''後。但只有當這是最終的'。'時,例如,我不希望它意外地捕捉到baby.dinosoars.txt中的「dinosoars」。
[Checkingtofileextensions](http://stackoverflow.com/questions/5899497/checking-file-extension) –