How do you move to a different folder in python?
You may use the following template to move a file in Python: Show import shutil original = r'original path where the file is currently stored\file name.file extension' target = r'target path where the file will be moved\file name.file extension' shutil.move(original, target) Alternatively, you may use this template to move a directory: import shutil original = r'original path where the directory is currently stored\directory name' target = r'target path where the directory will be moved\directory name' shutil.move(original, target) Let’s now review some examples with the steps to move your file or directory in Python. Step 1: Capture the Original PathTo begin, capture the original path where your file is currently stored. For example, let’s suppose that a CSV file is stored in a folder called Test_1: C:\Users\Ron\Desktop\Test_1\my_csv_file.csv Where the file name is ‘my_csv_file’ and the file extension is csv. Step 2: Capture the Target PathNext, capture the target path where the file will be moved. For our example, let’s move the CSV file to a folder called Test_2: C:\Users\Ron\Desktop\Test_2\my_csv_file.csv Step 3: Move the File using PythonYou may now utilize this template to move the file to the target location: import shutil original = r'original path where the file is currently stored\file name.file extension' target = r'target path where the file will be moved\file name.file extension' shutil.move(original, target) Make sure to place the ‘r‘ character before each of your paths to avoid the following error: ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape For our example, the code to move the CSV file from the original location (i.e., Test_1) to the target location (i.e., Test_2) is as follows: import shutil original = r'C:\Users\Ron\Desktop\Test_1\my_csv_file.csv' target = r'C:\Users\Ron\Desktop\Test_2\my_csv_file.csv' shutil.move(original, target) Once you run the code in Python (adjusted to your paths), the CSV file will be moved to the Test_2 folder. Rename the File when Moving itAlternatively, you can rename your file when you move it to your target location. For instance, let’s suppose that a new JPG file is stored in the Test_1 folder (where the file name is image). The code below can then be used to move the file (with the original file name of ‘image‘) to the target location with a new file name (‘new_image‘): import shutil original = r'C:\Users\Ron\Desktop\Test_1\image.jpg' target = r'C:\Users\Ron\Desktop\Test_2\new_image.jpg' shutil.move(original, target) The file, with the new name, should now appear in the Test_2 folder. Move a Directory using PythonSo far, you have seen how to move a file in Python. Alternatively, you may move a directory using this template (without specifying any file extension): import shutil original = r'original path where the directory is currently stored\directory name' target = r'target path where the directory will be moved\directory name' shutil.move(original, target) For instance, let’s say that a new directory was added to the Test_1 location, where the directory name is my_folder. Therefore, the following code can be used to move the directory to the Test_2 target location: import shutil original = r'C:\Users\Ron\Desktop\Test_1\my_folder' target = r'C:\Users\Ron\Desktop\Test_2\my_folder' shutil.move(original, target) The directory would now appear under the target location. You just saw how to move a file in Python using shutil.move. You may also want to check the following guide that explains how to copy a file in Python. This is what I'm using at the moment:
Now fully functional. Hope this helps you. Edit:I've turned this into a function, that accepts a source and destination directory, making the destination folder if it doesn't exist, and moves the files. Also allows for filtering of the src files, for example if you only want to move images, then you use the pattern
Can you move files with Python?Python provides functionality to move files or directories from one location to another location. This can be achieved using shutil. move() function from shutil module.
How do I navigate from one folder to another?You can move a file or folder from one folder to another by dragging it from its current location and dropping it into the destination folder, just as you would with a file on your desktop. Folder Tree: Right-click the file or folder you want, and from the menu that displays click Move or Copy.
How do I move files into a folder?Move files from Categories section. On your Android device, open Files by Google .. At the bottom, tap Browse .. Under "Categories," select a category.. Find the files you want to move. To move one file: Next to the file, tap More. . ... . Tap Internal storage.. Choose the folder you want to move the file to.. Tap Move here.. |