A new file is stored in one location. Write a code snippet that moves it to a different target location.
1Times asked
Jun 2026Last seen
Jun 2026First seen
💡 Model Answer
In Python you can move a file using the shutil module. The move operation is atomic on most file systems and preserves metadata. Example:
python
import shutil
source = "/path/to/source/file.txt"
target = "/path/to/target/file.txt"
shutil.move(source, target)If you need to handle errors, wrap in a try/except block. Complexity is O(1) for the move call, but the underlying copy may be O(n) in bytes if the file system does not support atomic moves across different devices. This snippet works on Windows, macOS, and Linux.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500