Move & Rename Files Using Python

Python

An example of using the os rename function to copy a file to a different location and change the name, in other words to change the file path using Python. In this instance the sales.csv file is renamed to BookSales.csv and is copied from the data folder to the archive folder.

 1|  import os
 2|  
 3|  original_file_path = 'D:\data\sales.csv'
 4|  new_file_path = 'D:\archive\BookSales.csv'
 5|  
 6|  os.rename(original_file_path,new_file_path)
Did you find this snippet useful?

Sign up for free to to add this to your code library