擅长:python、mysql、java
<p>一个好的开始是验证输入。换句话说,您可以确保用户确实为真实的现有文件键入了正确的路径,如下所示:</p>
<pre><code>import os
fileName = input("Please enter the name of the file you'd like to use.")
while (not os.path.isFile(fileName)) or (not os.path.exists(fileName)):
fileName = input("Whhoops! No such file! Please enter the name of the file you'd like to use.")
</code></pre>
<p>这需要内置模块<a href="http://docs.python.org/2/library/os.html" rel="nofollow">os</a>的一些帮助,该模块是标准Python库的一部分。</p>