执行Python脚本出现NativeCommandError,请求排查代码及环境问题
Hey there, let's dig into what's causing this issue with your code! First off, I spotted a straightforward syntax mistake that's almost certainly the root of the problem you're seeing.
The Critical Syntax Error
Take a look at this line in your script:
matches.append(os.path.join(root, filename)
You're missing a closing parenthesis at the end! It should be written as:
matches.append(os.path.join(root, filename))
This syntax error stops the Python interpreter from properly parsing and running your script. On Windows (especially in PowerShell), when a Python script fails to execute due to a syntax issue, it can sometimes get wrapped into a NativeCommandError instead of showing the direct Python syntax error message—this is why you're seeing that specific error type.
Checking Other Potential Factors
You mentioned you've set up Notepad++ with 4-space indents (no tabs) using the Python indent plugin, which is great—indentation isn't the problem here. That part of your setup is totally correct.
Corrected Full Code
Here's the fixed version of your script, with a small readability improvement:
import os import fnmatch # Replace these with your actual values rootPath = "C:/your/target/directory" pattern = "*.txt" # Example pattern, adjust to your needs matches = [] for root, dirs, files in os.walk(rootPath): for filename in fnmatch.filter(files, pattern): full_file_path = os.path.join(root, filename) matches.append(full_file_path) print(full_file_path)
Additional Checks (If the Error Persists)
If fixing the syntax error doesn't resolve the NativeCommandError, double-check these details:
- Ensure
rootPathis a valid directory you have read access to on your system. - Verify your
patternuses correct fnmatch syntax (e.g.,*.pyfor Python files,*log*.csvfor CSV files with "log" in the name). - Make sure you're running the script with the right terminal command—use
python your_script.pyorpy your_script.py(Windows-specific shorthand) to launch it.
内容的提问来源于stack exchange,提问作者MaxRussell

