I placed another project on github. These are 3 simple games made using Visual Studio Express 2010/Visual Basic 2010. Complete working code.
I placed the project for which I had made my old blog on github. I had the versions backed up but not via git. Back then I didn’t know about versioning software like git and mercurial. I was cleaning up my codebase and thought that converting the code dumps of previous versions of the software into git would make it much easier to manage the whole thing if I needed.
I also added the C++ programs’s source code which I had used for the project into the github repository. They had helped me back then take care of repetitive tasks a lot. That was kinda the project where I understood the real value of automating tasks and one of the reasons I started using Python.
I don’t think I am going to update it anymore but good to have that on github. It gets backed up. I am thinking about adding the setup files that I have for various versions on github or not. Still not decided on that.
If I ever thought of continuing the project I’ll do that from scratch in Python/PySide which I have been learning for some time.
If anyone’s interested in seeing what the project was just have a look at the old blog.
So Python is getting useful to me for avoiding manual repetitive tasks. I used it to rename a lot of folders. There were around 700 folders in 28 zip files which I needed to rename and re-zip. That would have been really cumbersome if not for Python.
The folders’ name were like 0001 – abc 1 . Only that the numbers didn’t match. The first number was used for arrangement and it was the second number according to which I had to change the first number. Doing that for 700 folders would have been a real pain. Luckily I knew Python and wrote a script for doing this.
Here’s the Python script that I used.
import zipfile import os import shutil CUR_DIR = os.getcwd() FILES_IN_CUR_DIR = os.listdir(CUR_DIR) def get_new_name(original_name): num = 0 started= False for c in original_name[7:]: if not c.isdigit(): if started is True: break continue else: started = True num *= 10 num += int(c) return str(num).zfill(4) + original_name[4:] def extract_files(zip_f, cur_path): with zipfile.ZipFile(zip_f, 'r') as zip_file: zip_file.extractall(cur_path) def rename_files(cur_path): for files in os.listdir(cur_path): new_name = get_new_name(files) os.rename(os.path.join(cur_path, files), os.path.join(cur_path, new_name)) def main(): files = [f for f in FILES_IN_CUR_DIR if f.endswith('.zip')] for f in files: cur_path = os.path.join(CUR_DIR, f[:-4]) print('Extracting ' + f) extract_files(f, cur_path) print('Started renaming') rename_files(cur_path) print('Removing zip file ', f) os.remove(os.path.join(CUR_DIR, f)) print('Writing back to ' + f) shutil.make_archive(cur_path, 'zip', cur_path) print('\n')if __name__ == "__main__": main()
This is just the first version that I used to get it to work. It creates temporary folders that I needed to delete but that was just one select and delete. Not a big thing. I plan to take care of the temporary folders too. The new one you can find in my github -> general -> Python33 -> Utilities maybe in a few days.
I have decided to use Python for automating some tasks that I do regularly and which consume a lot of time.
I have started with deleting useless files from my code base. I have been using github as a backup for my codebase. I need to upload the files on github server. When I am programming in C many executables and object files(.exe and .o) are generated. I need to delete these everytime before I commit my code base to github because backing up these files is of no use to me.
import os cur = os.getcwd() + "\\..\\..\\..\\git_repo\\C\\" extensions = ['.exe', '.o'] exclude = ['.git'] def delete_or_not(name): for i in extensions: if name.endswith(i): return True return False def cur_files(files): for name in files: if delete_or_not(name): yield name def main(startpath): for path, folders, files in os.walk(startpath, True): for folder in exclude: if folder in folders: folders.remove(folder) for name in cur_files(files): os.remove(path + "\\" + name)main(cur)
The script is simple. The things that anyone would need to change are at the beginning. These are cur, extensions and exclude.
- cur is the path of the folder, in which you start deleting files, relative to the script. If you want to put absolute path you need to take out os.getcwd(). Files in its subfolders are also deleted.
- extensions are the file extensions which you want to delete.
- exclude are the folder which you want to exclude from this deletion script’s scope. Generally you would want the hidden folders to be excluded. I included one folder which I did not want the script to go through.
After making these changes you can simply double click to delete files if you have Python installed. I use Python 2 but this should be easy to convert to Python 3 also.
Next time I’ll make a script to run a group of Python scripts. That’ll help me to make any number of different scripts and choose what I need to run at different times like at startup, shutdown, before uploading files etc.