使用命令行传入date参数时,Python脚本sys.argv[1]返回数字43263的原因
Hey there, let's break down why you're getting 43263 instead of the "date" argument you expected when running your script. Let's start with the most likely causes and how to check them:
1. You're not actually passing the string "date" as an argument
First, let's rule out the simplest possibility: maybe the way you're calling the script is accidentally passing a numeric date value instead of the literal string "date".
The biggest clue here: 43263 is an Excel-style date serial number
That number corresponds to August 31, 2018 (Excel counts days starting from January 0, 1900). This suggests your script is being called from a tool that treats dates as numeric values—like Excel, Access, or some automation software—rather than a plain terminal where you type python script.py date.
Quick test to confirm
Open your terminal directly and run:
python your_script_name.py date
If this outputs "date" as expected, the problem isn't with your script—it's with the way you're normally invoking it (e.g., via Excel VBA, a batch script that pulls a date value, etc.).
2. Check how you're invoking the script
If the terminal test works, dig into the environment where you usually run the script:
- Are you using Excel VBA to call the script? Double-check if you're passing a cell's value (which is a numeric date) instead of the string
"date". - Are you using a batch/shell script to trigger the Python code? Make sure there's no line that replaces
"date"with a numeric date value (like pulling a serial number from a database or spreadsheet).
3. Verify the full argument list
To be 100% sure what's being passed, modify your script to print all arguments:
import sys print("All arguments received:", sys.argv)
This will show you the entire list of arguments the script gets. If the first argument is 43263 instead of "date", you'll know the issue is definitely in how the parameter is being sent to the script.
4. Edge case: sys.argv was modified (unlikely but possible)
While your script is super simple, there's a tiny chance that another module you're importing (even accidentally) is altering sys.argv. But this is rare—only consider this if the above steps don't uncover the issue.
内容的提问来源于stack exchange,提问作者SBad

