Can this be done in python and which module would i have to look at. I haven't experimented with this and if I have to guess would it be the os.renames() function.

Dani AI

Generated

Short answer: yes — Python can move files to a network drive. is right that a network path looks like a normal pathname to Python, and ’s quick test with os.renames() shows it can work in many cases. Two important caveats follow.

Renaming is atomic only when source and destination share the same filesystem. If the network share is mounted as a different device, a rename-based move may fail with an OSError. Use shutil.move which will automatically fall back to copy+delete when a simple rename is not possible; see the shutil docs for details: https://docs.python.org/3/library/shutil.html#shutil.move. The basic os.rename/os.replace behavior is documented here: https://docs.python.org/3/library/os.html#os.rename.

Practical tips and gotchas:

  • On Windows you can use mapped drives or UNC paths (\server\share\path). Be mindful of escaping backslashes in string literals and of the account used to run the script (permissions matter).
  • Network transfers are slower and less reliable than local moves. For large files, check free space and consider copying to a temporary filename and renaming when complete to avoid partial files.
  • Files can be locked by other processes; handle exceptions and retry if transient network errors occur.
  • For bulk or resumable transfers, prefer dedicated tools (rsync/robocopy) or implement checksums/partial-transfer logic in Python.

For robustness, wrap operations in try/except, log failures, and validate destination accessibility before starting large moves.

Recommended Answers

All 2 Replies

theoretically you should be able to, as the path to network drives is not that different from path to local drives.

well i wrote a test programme to check it out and it work with the os.renames() function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.