Question

a).Answer the question: where can you find the Standard Documentation for Python? b)Write an algorithm for...

a).Answer the question: where can you find the Standard Documentation for Python?

b)Write an algorithm for step 3.

c)Write a program that asks the user for a path to a directory, then updates the names of all the files in the directory that contain 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.

Homework Answers

Answer #1

a). The standard documentation can be found on their official website. A simple search in the search engine of your choice would show you the documentations.

c). I have used the "os" library to search and retrieve files in the directory and also to rename the files. "re" library is the regex library for python. I have used regex to find and replace the strings.

Below is the code with comments on every line:

Below are the files before the code execution:

Below are the files after the code execution:

Below is the screenshot of a file with edited date:

You can copy the code from here:

#CODE STARTS HERE-------------------------------------
import os   #Used to manipulate files in your os
import re  #Used to search and replace the strings in file name

dir_path=input("Enter the path to search\n") #Asking for user input

for root,dirs,files in os.walk(dir_path): 
   for file in files:
      if "draft" in file.lower(): 
         insensitive = re.compile(re.escape('draft'), re.IGNORECASE) 
         x=insensitive.sub('final',file) 
         src=os.path.join(dir_path,file) #preparing source path
         dest=os.path.join(dir_path,x)  #preparing destination path
         os.rename(src,dest)    #The file name is changed here

         #This adds the edited date inside the file
         import datetime
         dt_object = datetime.datetime.now()
         file_edit = open(dest, "a+")
         edit="EDITED ON "+str(dt_object)+"\n"
         file_edit.write(edit)
         file_edit.close()
#CODE ENDS HERE---------------------------------------
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT