How to add comment in each file of a folder-Python Code

This is another program in ‘Automation using Python“. In this series, we are writing python programs to automate some boring tasks.

Today we are going to add some comments in each python file at the beginning of the file. It is very simple to add comments in a single file but it is difficult to do when you have more than 4000 files in a directory and sub-directory.
add comment in each file of a fodler

In order to solve this issue, we have generated a simple python program to add comments in the beginning of the file.

Python program to add multiple comments at the beginning of the file. For this example,we have taken a python file abc.py having the following contents

for i in range(30):
    print(i)

Now we want to add the following lines at the beginning of this python file.

# program to comment on every file 
# made by       : rakesh kumar 
 # class -       : xi A 
 # session       :2019-20 

Python code to add comment at the beginning of file

str = "# program to comment on every file \n"
str = str + "# made by       : rakesh kumar \n"
str = str+" # class -       : xi A \n"
str = str+" # session       :2019-20 \n\n"

with open("C:/Users/rakesh/Desktop/PRATHAM/abc.py", "r") as f:
    data = f.read()

with open("abc.txt", "w") as f:
    f.write(str)
    f.write(data)

The output of the above program is stored at file ‘abc.txt’ file. The content of abc.txt file is as follows

# program to comment on every file 
# made by       : rakesh kumar 
 # class -       : xi A 
 # session       :2019-20 

for i in range(30):
    print(i)

Now everything is fine but the basic idea has been defeated. ie, we do not want a new file but want to add comments at the beginning. So where is the solution?
Solution is simple, Delete original file abc.py and rename abc.txt file as abc.py.

unlink("abc.py")
rename("abc.txt","abc.py")

but the above two python statements are not default statements. They are actually part of OS module.So all the time you have to import os module or import these two methods selectively.

Herer is the complete python code to add multi line comment in a file

from os import unlink, rename

str = "# program to comment on every file \n"
str = str + "# made by       : rakesh kumar \n"
str = str+" # class -       : xi A \n"
str = str+" # session       :2019-20 \n\n"

with open("C:/Users/rakesh/Desktop/PRATHAM/abc.py", "r") as f:
    data = f.read()

with open("abc.txt", "w") as f:
    f.write(str)
    f.write(data)

unlink("abc.py")
rename("abc.txt", "abc.py")

It is fine that you are able to add a comment in any give python file using the above python code. What about all files available in a folder or in all subfolders in the given folder.

In order to achieve this, we have to use os.walk() that will check all the files and folders available in a folder and then based on that we will apply the above code to add a multi-line comment in that file.

Other useful Python Programs

  1. Delete all Duplicate files from a folder and all in its subfolder
  2. Compress files removing spaces and tabs

Complete code to add comments in all the files of a folder using Python.

# program to add comment at the begining of each python file having extension .py
# made by : rakesh kumar
# licence : MIT

import os
import sys # module for terminating
import glob
import tkinter as tk
from tkinter import filedialog

str = “# program to comment on every file \n”
str = str + “# made by : rakesh kumar \n”
str = str+” # class – : xi A \n”
str = str+” # session :2019-20 \n\n”

def add_comment(filename):
with open(filename, “r”) as f:
data = f.read()

with open(“abc.txt”, “w”) as f:
f.write(str)
f.write(data)

os.unlink(filename)
os.rename(“abc.txt”, filename)

if __name__ == “__main__”:
root = tk.Tk()
root.withdraw()
directory = filedialog.askdirectory() # source folder
count = 0
for root, SubFolders, files in os.walk(directory):
os.chdir(root)
files = glob.glob(‘*.py’)
# print(files)
for filename in files:
print(filename, ‘Comment Added’)
add_comment(filename)
count += 1

Enjoy. Hope you have learn a lot to ‘automate using python’ through this program.

Related Posts

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.