Python 2.7及XCOPY复制本地到网络存储G-Code文件不完整问题
Let's break down why your 103KB G-Code TXT file keeps truncating at line 6285 (mid-way through "Z17.168 Y7.393" to "Z17.168 Y7.3") and how to fix it. This consistent truncation at the exact spot points to a predictable issue, not random network flakiness.
Possible Root Causes
- Network Storage Cache/Flush Delay: Many network drives use write caching to boost performance. If the system assumes the copy is complete before the cache finishes writing to disk, you'll end up with a truncated file.
- Hidden File Anomalies: A non-ASCII control character or malformed line ending near line 6285 might be causing the write operation to abort prematurely.
- Limitations of Basic Copy Tools: XCOPY and naive Python file copies often don't handle network storage quirks (like partial writes) as reliably as purpose-built tools.
Step-by-Step Fixes
1. Verify the Source File is Intact
First, rule out a corrupted source file:
- Copy the file to a local folder (not network storage) using:
copy "C:\path\to\source.txt" "C:\temp\test.txt" - Compare the two files with a binary check to ensure they match:
fc /B "C:\path\to\source.txt" "C:\temp\test.txt" - Alternatively, verify MD5 hashes to confirm integrity:
certutil -hashfile "C:\path\to\source.txt" MD5 certutil -hashfile "C:\temp\test.txt" MD5
If the checks pass, the source file is fine—your problem lies with the network storage.
2. Use Robocopy Instead of XCOPY
Robocopy is designed for reliable network transfers. Try this command:
robocopy "C:\source\folder" "\\network\drive\folder" "your-file.txt" /Z /R:3 /W:5
/Z: Enables restartable mode to resume interrupted transfers/R:3: Retries 3 times if a write fails/W:5: Waits 5 seconds between retries
3. Fix Your Python Copy Code
If you prefer using Python, add explicit buffer flushing and disk sync to ensure all data is written to the network drive. For Python 2.7, use this modified function:
import os def safe_copy(src_path, dst_path): buffer_size = 1024 * 1024 # Use 1MB chunks for efficient transfer with open(src_path, 'rb') as src_file: with open(dst_path, 'wb') as dst_file: while True: chunk = src_file.read(buffer_size) if not chunk: break dst_file.write(chunk) dst_file.flush() # Push data from OS buffer to network drive os.fsync(dst_file.fileno()) # Force network drive to write to disk
4. Check for Hidden Characters in the Source File
Open your G-Code file in Notepad++ (enable "Show All Characters" via View > Show Symbol > Show All Characters) and inspect line 6285 and nearby lines. Look for:
- Non-printable control characters (like
^@or other odd symbols) - Malformed line endings (e.g., only LF instead of CRLF)
If you find any anomalies, clean up the line and try copying again.
5. Wait for Cache Flush After Copy
After any copy operation, don't immediately disconnect the network drive or close the terminal. Wait 1-2 minutes to give the network storage time to write cached data to disk. Some drives have a LED indicator that stops blinking when writes are complete—wait for that signal.
6. Check Network Storage Health & Settings
- Ensure the network drive has enough free space (even small files need room for temporary writes)
- If using SMB, try upgrading to SMB 3.0 (Windows: Control Panel > Network and Sharing Center > Change advanced sharing settings > Enable SMB 3.0)
- Confirm the network storage has no unexpected file size limits (unlikely for 103KB, but worth checking)
内容的提问来源于stack exchange,提问作者m0h

