In Python. Write an algorithm for the following question. Write a program that a
ID: 3852647 • Letter: I
Question
In Python.
Write an algorithm for the following question.
Write a program that asks the user for a path to a directory, and then updates the names of all the files in the directory that have the word draft to instead say final
EX: "term paper (draft).txt" would be renamed "term paper (final).txt"
BONUS (5pts): for any .txt file that your program changes the name of, have your program add a line of text that states "Edited on " followed by the current date to the end of the text in the file that it is editing.
Explanation / Answer
import glob, os
import fnmatch
def rename(dir, name,newName):
for filename in os.listdir(dir): #opening file names
if fnmatch.fnmatch(filename, name): #matching file names
os.rename(filename, newName) # renaming file names
rename(r'c: empmyFolder', 'draft', 'final')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.