How to write tag deleter script in python
Question: I want to implement a file reader (folders and subfolders) script which
detects some tags and delete those tags from the files.

The files are .cpp, .h .txt and .xml And they are hundreds of files under same
folder.

I have no idea about python, but people told me that I can do it easily.

**EXAMPLE** :

My main folder is A: C:\A

Inside A, I have folders (B,C,D) and some files A.cpp A.h A.txt and A.xml. In
B i have folders B1, B2,B3 and some of them have more subfolders, and files
.cpp, .xml and .h....

  * xml files, contains some tags like `<!-- $Mytag: some text$ -->`

  * .h and .cpp files contains another kind of tags like `//$TAG some text$`

  * .txt has different format tags: `#$This is my tag$`

It always starts and ends with $ symbol but it always have a comment character
(//,

The idea is to run one script and delete all tags from all files so the script
must:

  * Read folders and subfolders
  * Open files and find tags 
  * If they are there, delete and save files with changes

**WHAT I HAVE:**

    
    
    import  os
    
    for root, dirs, files in os.walk(os.curdir):
    
     if files.endswith('.cpp'):
      %Find //$ and delete until next $
     if files.endswith('.h'):
      %Find //$ and delete until next $
     if files.endswith('.txt'):
      %Find #$ and delete until next $
     if files.endswith('.xml'):
      %Find <!-- $ and delete until next $ and -->
    


Answer: The general solution would be to:

  1. use the `os.walk()` function to traverse the directory tree. 
  2. Iterate over the filenames and use `fn_name.endswith('.cpp')` with if/elseif to determine which file you're working with
  3. Use the `re` module to create a regular expression you can use to determine if a line contains your tag
  4. Open the target file and a temporary file (use the `tempfile` module). Iterate over the source file line by line and output the filtered lines to your tempfile. 
  5. If any lines were replaced, use `os.unlink()` plus `os.rename()` to replace your original file 

It's a trivial excercise for a Python adept but for someone new to the
language, it'll probably take a few hours to get working. You probably
couldn't ask for a better task to get introduced to the language though. Good
Luck!

\----- Update -----

The `files` attribute returned by os.walk is a list so you'll need to iterate
over it as well. Also, the `files` attribute will only contain the base name
of the file. You'll need to use the `root` value in conjunction with
`os.path.join()` to convert this to a full path name. Try doing just this:

    
    
    for root, d, files in os.walk('.'): 
        for base_filename in files: 
            full_name = os.path.join(root, base_filename)
            if full_name.endswith('.h'):
                print full_name, 'is a header!'
            elif full_name.endswith('.cpp'):
                print full_name, 'is a C++ source file!'
    

If you're using Python 3, the print statements will need to be function calls
but the general idea remains the same.

