Mini Tools that Make Daily Works Simple

Constantly ran into some redundant works lately, like renaming folders/files, modifying file properties, etc. Hence, these mini tools.

Well, I'm no good at .bat scripts. Which, doesn't require basically anything, and is much easier to use.

#Find all files in the folder

And files in sub-folder, sub-sub-folder,...

1
2
3
4
5
6
7
8
9
10
def all_files_path(root_dir):
filepaths = []
for root,dirs,files in os.walk(root_dir):
for file in files:
file_path = os.path.join(root,file)
filepaths.append(file_path)
for dir in dirs:
dir_path = os.path.join(root,dir)
all_files_path(dir_path)
return filepaths

The return value of the function above is a list of strings which contain the full path of the files under folder root_dir.

With this function, there're so many stuff you get to do. For example, to delete all .un~ files under the current folder, simply pick all strings ending with '.py~', then use os.unlink to delete corresponding files. With regular expressions, there're even more fun stuffs you can do.

#Deleting EXIF information

EXIF may contain a lot of sensitive information such as where and when the picture was taken, what kind of camera was used, plus even detailed parameters of a picture, ISO, aperture, etc. Therefore deleting EXIF information is widely suggested before uploading pictures to the internet or sending pictures to other people, even the people you know.

Below I'll use only .jpg files to demonstrate how it's done.

1
2
3
4
5
6
7
from pyexiv2 import Image
import json

img = Image(f"{imgFileName}")
exif = img.read_exif()
with open(f"{jsonFileName}", 'w') as f:
json.dump(exif, f, indent=2)

{
"Exif.Image.Make": "Canon",
"Exif.Image.Model": "Canon EOS 800D",
"Exif.Image.Orientation": "1",
"Exif.Image.XResolution": "72/1",
"Exif.Image.YResolution": "72/1",
"Exif.Image.ResolutionUnit": "2",
"Exif.Image.DateTime": "2018:09:12 15:15:50",
...
}

Basically, EXIF is stored in the shape of a dictionary. Modifying all the values to whatever don't make any sense would just do the trick.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from pyexiv2 import Image

def all_files_path(root_dir): # still remember this function, do you?
...

if __name__ == "__main__":
all_files_path(os.getcwd())
for filepath in filepaths:
if filepath.endswith(".JPG") or filepath.endswith(".jpg"):
img = Image(filepath)
exif = img.read_exif()
for i in exif:
exif[i] = ""
img.modify_exif(exif)

If you read the exif information now, it is gonna be like below.

{
"Exif.Image.Make": "",
"Exif.Image.Model": "",
"Exif.Image.Orientation": "",
"Exif.Image.XResolution": "",
"Exif.Image.YResolution": "",
"Exif.Image.ResolutionUnit": "",
"Exif.Image.DateTime": "",
...
}

#Open file with a certain application via console

Some times using console to start an application and open certain files would do a lot of benefit.

Here the script contains file renaming and processing all files under a perticular folder, combined with Find all files in the folder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if __name__ == "__main__":
path = input("folder name: (folder containing all files)\n")
ULTRA = '"{application executable file path}"'
l = all_files_path(path)
ext1 = ["docx", "doc"]
ext2 = ["txt", "html"]
for i in l:
print(i)
for j in range(len(ext1)):
if i.endswith(ext1[j]):
os.rename(i, f"""{".".join(i.split(".")[:-1])}.{ext2[j]}""")
targetName = f"{'.'.join(i.split('.')[:-1])}.{ext2[j]}"
os.system(f'{ULTRA} {targetName}')
elif i.endswith(ext2[j]):
os.rename(i, f"""{".".join(i.split(".")[:-1])}.{ext1[j]}""")

#Displace Curves with matplotlib

#Afterword

More yet to come. =.=

The year 2022 Review on the Firsts in Life
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×