Windows 10下Python 3.7.4 pickle存aux开头文件报FileNotFoundError原因
Hey there! Let's dig into why you're running into this frustrating FileNotFoundError when trying to save pickle files with names starting with aux on Windows 10. It's not a Python or pickle bug—this is a Windows system-level restriction rooted in legacy DOS naming rules.
The Root Cause: Windows' Reserved Device Names
Back in the DOS days, certain short names were reserved to refer to system devices (like serial ports, printers, or the null device). Windows kept this compatibility for backward compatibility, so even today, you can't use these names as file or folder names—even if you add an extension (like .pk or .foo).
aux is one of these reserved names. Other common ones include:
con(console input/output)prn(printer)nul(null device)com1tocom9(serial ports)lpt1tolpt9(parallel ports)
When you try to open a file named aux.pk in Python, the Windows API doesn't treat it as a regular file—it thinks you're trying to access the legacy aux device, which doesn't exist as a file on your system. That's why you get a FileNotFoundError, even though you're trying to create a new file.
Your Test Cases Make Perfect Sense Now
Looking at your code examples:
- All attempts using
aux(with any extension, raw string, or formatted string) fail because Windows blocks the name entirely. - Switching to
tuxworks because it's not a reserved device name—Windows treats it as a normal file, so pickle can save it without issues.
Quick Recap
This is purely a Windows compatibility quirk. Python's open() function relies on Windows' underlying file handling APIs, so it has to respect these old naming rules. The workaround you found (using a non-reserved filename) is the simplest and most reliable fix.
内容的提问来源于stack exchange,提问作者Alexandre Cavalcante

