Python: Move Files From One Directory To Another

How do you move files from one directory to another using Python? If you want to move specific files from one directory to another with Python, use the glob library to fetch the correct files, then use the os library’s rename method to change the current file to the new directory. Here is an example of how this might look using a Python script. 1 2 3 4 5 import os import glob current_dir = os.getcwd() old_files = os.path.join(current_dir, 'old_loc', '*.py') With the current code I have imported the 2 essential libraries needed to help co-ordinate the change, these being the os and glob libraries. ...

September 20, 2022 · 4 min · 799 words · Ryan Sheehy