擅长:python、mysql、java
<p>对于这种类型的东西,你需要注意你的实际工作目录是什么。例如,不能从文件所在的目录运行脚本。在这种情况下,不能只使用相对路径本身。</p>
<p>如果您确定所需文件位于脚本实际所在的子目录中,则可以使用<code>__file__</code>来帮助您完成此任务。<code>__file__</code>是运行的脚本所在的完整路径。</p>
<p>所以你可以摆弄这样的东西:</p>
<pre><code>import os
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
</code></pre>