Fix diskpart script file not found error (WinError 2)

- Replace tempfile.NamedTemporaryFile with manual file creation in Windows temp directory
- Use Windows TEMP/TMP environment variables for script location
- Add file existence verification before running diskpart
- Enhanced error handling for FileNotFoundError and subprocess errors
- Robust cleanup that won't fail if file deletion fails
- Add specific handling for diskpart exit code 2 (file not found)
- Resolves '[WinError 2] the system cannot find the file specified' error
parent 46fb0ab1
......@@ -275,13 +275,25 @@ assign letter={usb_drive[0]}
exit
"""
# Write diskpart script to temporary file
script_path = None
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as script_file:
script_file.write(diskpart_script)
script_path = script_file.name
# Write diskpart script to a safe temporary location
import tempfile
import os
# Use Windows temp directory and ensure file is accessible
temp_dir = os.environ.get('TEMP', os.environ.get('TMP', 'C:\\Windows\\Temp'))
script_path = os.path.join(temp_dir, f'diskpart_script_{os.getpid()}.txt')
try:
# Write script file
with open(script_path, 'w') as script_file:
script_file.write(diskpart_script)
# Ensure file exists and is readable
if not os.path.exists(script_path):
raise Exception(f"Failed to create diskpart script at {script_path}")
self.status.emit(f"Created diskpart script: {script_path}")
# Run diskpart to format the USB drive
self.status.emit("Formatting USB drive...")
result = subprocess.run([
......@@ -289,8 +301,11 @@ exit
], capture_output=True, text=True, timeout=60)
# Clean up diskpart script
os.unlink(script_path)
script_path = None
try:
os.unlink(script_path)
script_path = None
except:
pass # Don't fail if cleanup fails
# Check if diskpart succeeded
if result.returncode != 0:
......@@ -316,15 +331,30 @@ exit
except subprocess.TimeoutExpired:
if script_path and os.path.exists(script_path):
os.unlink(script_path)
try:
os.unlink(script_path)
except:
pass
raise Exception("USB formatting timed out. This may happen with slow USB drives or if the drive is in use. Please try again or use a different USB drive.")
except FileNotFoundError as e:
if script_path and os.path.exists(script_path):
try:
os.unlink(script_path)
except:
pass
raise Exception(f"Diskpart command not found or script file inaccessible. Please ensure you're running as administrator and diskpart is available. Error: {e}")
except subprocess.CalledProcessError as e:
if script_path and os.path.exists(script_path):
os.unlink(script_path)
try:
os.unlink(script_path)
except:
pass
# Handle specific diskpart error codes
if e.returncode == 2147755049 or e.returncode == 0x80070079:
raise Exception("USB drive access timeout. Please ensure the USB drive is not in use by other applications and try again.")
elif e.returncode == 2: # File not found
raise Exception("Diskpart script file not found. This may be a temporary file access issue. Please try again.")
elif "Access is denied" in str(e) or "elevation" in str(e).lower():
raise Exception("Administrator privileges required. Please run the application as administrator.")
else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment