os.rename(), os.replace(), or shutil.move()

All employ the same syntax:

import os
import shutil

os.rename("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
os.replace("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
  • The filename ("file.foo") must be included in both the source and destination arguments. If it differs between the two, the file will be renamed as well as moved.
  • The directory within which the new file is being created must already exist.
  • On Windows, a file with that name must not exist or an exception will be raised, but os.replace() will silently replace a file even in that occurrence.
  • shutil.move simply calls os.rename in most cases. However, if the destination is on a different disk than the source, it will instead copy and then delete the source file.
Answer from ig0774 on Stack Overflow
🌐
Medium
medium.com › @AlexanderObregon › javas-files-move-method-explained-7dee1287fa92
Java’s Files.move() Method Explained | Medium
September 14, 2024 - By default, if a file already exists at the target location, the Files.move() method will throw an IOException and fail to move the file. However, in situations where you want to replace the existing file with the new one, the StandardCopyOption.REPLACE_EXISTING option can be used to instruct the method to overwrite the target file. Here’s an example of how to move a file and replace any existing file at the target location:
🌐
GeeksforGeeks
geeksforgeeks.org › c# › file-move-method-in-c-sharp-with-examples
File.Move() Method in C# with Examples - GeeksforGeeks
April 21, 2020 - Moved After running the above code, above output is shown and the existing file file.txt moved to a new location C:\gfg.txt which is shown below- Program 2: Initially no file was created.
🌐
Baeldung
baeldung.com › home › java › java io › java – rename or move a file
Rename or Move a File in Java | Baeldung
January 5, 2024 - In the examples, we’ll use the following setup, which consists of 2 constants for the source and destination file name and a clean-up step to be able to run the tests multiple times: private final String FILE_TO_MOVE = "src/test/resources/originalFileToMove.txt"; private final String TARGET_FILE = "src/test/resources/targetFileToMove.txt"; @BeforeEach public void createFileToMove() throws IOException { File fileToMove = new File(FILE_TO_MOVE); fileToMove.createNewFile(); } @AfterEach public void cleanUpFiles() { File targetFile = new File(TARGET_FILE); targetFile.delete(); }
🌐
Jenkov
jenkov.com › tutorials › java-nio › files.html
Java NIO Files
April 15, 2015 - First the source path and destination path are created. The source path points to the file to move, and the destination path points to where the file should be moved to. Then the Files.move() method is called.
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › visual-basic › developing-apps › programming › drives-directories-files › how-to-move-a-file
How to: Move a File - Visual Basic | Microsoft Learn
Use the MoveFile method to move the file, specifying the source file name and location, the target location, and the new name at the target location. This example moves the file named test.txt from TestDir1 to TestDir2 and renames it nexttest.txt.
🌐
Opensource.com
opensource.com › article › 21 › 8 › move-files-linux
Move files in the Linux terminal | Opensource.com
To move a file in a terminal, you use the mv command to move a file from one location to another. $ mv example.txt ~/Documents $ ls ~/Documents example.txt
🌐
Microsoft Learn
learn.microsoft.com › en-us › windows-server › administration › windows-commands › move
move | Microsoft Learn
To move all files with the .xls extension from the \Data directory to the \Second_Q\Reports directory, type:
🌐
Mkyong
mkyong.com › home › java › how to move file to another directory in java
How to move file to another directory in Java - Mkyong.com
August 4, 2020 - Files.move – Move file in local system. ... This Java example uses NIO Files.move to move a file from to another directory in the same local drive.
🌐
datagy
datagy.io › home › file handling › how to move files in python (os, shutil)
How to Move Files in Python (os, shutil) • datagy
February 10, 2023 - In this section, we’ll explore how to use the three libraries to move a file using Python. We’ll dive into more detail in this section but then rely only on the shutil.move() function for the remainder of the tutorial. The processes are largely the same, but feel free to leave a comment if you run into any issues. For all of the examples, let’s imagine that we have a file in a folder at '/Users/datagy/file.txt'.
🌐
Computer Hope
computerhope.com › movehlp.htm
Move Command
March 21, 2025 - Move the files of c:\windows\temp to the temp directory in root, this is of course assuming you have the Windows\temp directory. In this example, *.* is wildcards telling the computer every file with every extension.
🌐
PYnative
pynative.com › home › python › file handling › move files or directories in python
Python Move Files Or Directories [5 Ways]– PYnative
January 19, 2022 - Now use the shutil.move() method to move the current file to the destination folder path. Example: Move all files from the report folder into a account folder.
🌐
Computer Hope
computerhope.com › issues › ch001476.htm
How to Move Files and Folders on the Computer
In the Windows command line and MS-DOS, you can move files using the move command. For example, to move a file named "stats.doc" to the "c:\statistics" folder, you would type the following command, then press the Enter key.
🌐
FavTutor
favtutor.com › blogs › move-file-python
3 Ways to Move File in Python (Shutil, OS & Pathlib modules)
January 2, 2024 - import shutil shutil.move("C:\files\example.txt", "C:\backup\example.
🌐
LinuxVox
linuxvox.com › blog › move-files-linux
Mastering File Movement in Linux: A Comprehensive Guide — linuxvox.com
To move a single file, simply specify the source file and the destination directory or file name. For example, to move a file named example.txt from the current directory to the documents directory, you can use the following command: