You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Sublime Text 3中运行Python 3.6代码遇File Not Found Error求助

Troubleshooting FileNotFoundError [Errno 2] in Sublime Text 3 with Python 3.6

Hey there, let's work through this FileNotFoundError you're facing—even when you've double-checked folder locations, there are a few sneaky issues that often cause this. Here are the most likely fixes:

1. Sublime Text's working directory isn't your script's folder

By default, Sublime might run your Python script from a directory you don't expect (like its own install folder or your user home directory), not the folder where your script lives. That means any relative paths in your code will look in the wrong place.

Fix:

Update your Python build system to set the working directory to your script's path:

  • Go to Tools > Build System > New Build System
  • Replace the default content with this JSON:
{
    "cmd": ["python3", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "working_dir": "${file_path}",
    "selector": "source.python",
    "env": {"PYTHONIOENCODING": "utf-8"}
}
  • Save it as Python3_Custom.sublime-build (or any name you like)
  • Select this new build system from Tools > Build System and run your script again.

2. Relative paths in your code aren't pointing where you think

If your code uses relative paths (like open("my_file.txt")), Python will look for that file in the current working directory (not necessarily the folder where your script is saved).

Fix:

Modify your code to use absolute paths based on your script's location:

import os

# Get the absolute path of your script's folder
script_folder = os.path.dirname(os.path.abspath(__file__))
# Combine it with your target filename
target_file = os.path.join(script_folder, "my_file.txt")

# Now open the file using the absolute path
with open(target_file, "r") as f:
    # Your code here

3. Case sensitivity or hidden file extensions

  • Case mismatch: On Linux/macOS (and sometimes even in Sublime's cross-platform setup), filenames are case-sensitive. If your code references Data.txt but the actual file is data.txt, you'll get this error. Double-check the exact spelling and capitalization.
  • Hidden extensions: On Windows, default settings hide known file extensions. So a file named data.txt.txt will show up as data.txt in File Explorer—but Python will look for the exact name you specified. Enable "File name extensions" in the View tab of File Explorer to verify.

4. Permissions (less common but worth checking)

In rare cases, you might have the correct path but lack permission to access the file or folder. On Linux/macOS, run chmod +r my_file.txt to grant read access. On Windows, right-click the file/folder, go to Properties > Security and ensure your user account has read permissions.

Start with the first two fixes—they're the most common culprits when you've already confirmed folders are in the right place.

内容的提问来源于stack exchange,提问作者Rubyink

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:33:55