Python新手运行500px-Bot代码遭遇JSONDecodeError求助
Hey there! Let's work through that JSON decode error you're hitting with the 500px bot script. That error json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) almost always means the script is trying to parse something that isn't valid JSON—most often an empty file or malformed content. Here are the steps to fix it:
1. Check if your pendingUsers.txt file is empty or missing
If you're running the script for the first time, pendingUsers.txt might not exist at all, or it could be an empty file. JSON can't parse an empty string, so that's a super common culprit here.
To fix this, add a quick check before loading the file to handle empty or missing cases:
import os import json # Handle pendingUsers.txt initialization pending_file = 'pendingUsers.txt' if not os.path.exists(pending_file) or os.path.getsize(pending_file) == 0: pendingFollowList = [] else: with open(pending_file, 'r', encoding='utf-8') as f: pendingFollowList = json.loads(f.read())
This way, if the file doesn't exist or is empty, we start with an empty list instead of throwing an error.
2. Verify the file has valid JSON content
If the file exists and has content, open it manually (with Notepad or VS Code) to check for:
- Missing brackets or quotes (like
[1,2,3instead of[1,2,3]) - Non-JSON text (random characters or plain text that isn't formatted properly)
- Garbled text from encoding mismatches (even with
utf-8, if the file was saved with a different encoding, you might get unreadable content)
If you edited the file manually, double-check that you're using proper JSON syntax. If the script writes to this file, make sure it uses json.dump() instead of plain string writes—json.dump() automatically handles correct formatting.
3. Confirm the file path is correct (Windows quirk)
On Windows, relative file paths depend on the working directory where you run the script. If you're running the script from a different folder than where pendingUsers.txt is stored, the script will create a new empty file in the working directory instead of using the existing one.
To check your current working directory, add this line to your script:
import os print("Current working directory:", os.getcwd())
If the path doesn't match where your pendingUsers.txt is located, either:
- Move the file to the working directory, or
- Use an absolute path to open the file (replace the example path with your actual folder):
with open(r'C:\Users\YourName\Scripts\pendingUsers.txt', 'r', encoding='utf-8') as f: pendingFollowList = json.loads(f.read())
Try these steps one by one—chances are the empty file or path mismatch is the root cause here!
内容的提问来源于stack exchange,提问作者Alen Ćemer

