Home โ€บ Interview Questions โ€บ File I/o, Python

A new file is stored in one location. Write a code snippet that moves it to a different target location.

๐ŸŸข Easy Coding Fresher level
1 Times asked
Jun 2026 Last seen
Jun 2026 First 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.

Sign in to unlock the rest of this answer

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 on a discreet on-screen overlay.

Get Assisting AI โ€” Starts at โ‚น500