The task you have to perform is "Oh Soldier Prettify my Folder."
Suppose you have a folder, and its path is also given. You have to create a function which takes three input arguments, which are:
def soldier("C://", "harry.txt", "jpg")
The function will perform three tasks:
After performing these tasks, your folder will prettify as all the first letters of the files in the folder will be capitalize except for those files whose names are present in the dictionary file. And the files having the same format as given in the input argument will rename to number in the range of 1-100.
The solution is given in tutorial#88.
# Oh soldier Prettify my Folder
# path, dictionary file, format
# def soldier("C://", "harry.txt", "jpg")
import os
def soldier(path, file, format):
os.chdir(path)
i = 1
files = os.listdir(path)
with open(file) as f:
filelist = f.read().split("\n")
for file in files:
if file not in filelist:
os.rename(file, file.capitalize())
if os.path.splitext(file)[1] == format:
os.rename(file, f"{i}{format}")
i +=1
soldier(r"C:\Users\Haris\Desktop\testing",
r"C:\Users\Haris\PycharmProjects\PythonTuts\ext.txt", ".png" )
No downloadable resources for this video. If you think you need anything, please post it in the QnA!
Any Course related announcements will be posted here