Exercise 51

By using the os.rename() method, create a python program to rename an existing file on the desktop  called myfile1.txt to myfile2.txt. (you must create the file called myfile1.txt manually on the desktop)


import os
# retrieve username by using the getlogin() method
user = os.getlogin()
#define file path for the existing file in desktop
file1 = "C:/Users/" + user + "/Desktop/myfile1.txt"

# Rename the file1
os.rename(file1, "C:/Users/" + user + "/Desktop/myfile2.txt")
Younes Derfoufi
One thought on “Solution Exercice 51: rename the existing file”

Leave a Reply