-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (63 loc) · 2.16 KB
/
main.py
File metadata and controls
77 lines (63 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#! /bin/bin/env python
import magic, os, time
def main():
#List files in current directory
path = os.getcwd()
#Set a counter for files and renames
fcount = 0 #Count of all non-directory files
rcount = 0 #Count of renamed files
begin = False #Run program -- default: False
out = "" #Default empty output message
#Get user input
uin = input("Currently in: '" + str(path) + "'.\nDo you wish to check for bad file extensions? [y/n]")
if uin != "" and (uin.upper()[0]) == "Y":
begin = True
if begin:
#Print entry message
print("Please wait, this could take some time.")
#Walk through files
for filename in os.listdir(path):
#Do NOT walk directories
if os.path.isfile(filename):
fcount += 1
#Print regular updates, so the user has feedback
if fcount % 50 == 0:
print("Checked " + str(fcount) + " files so far...")
fn, ext = (os.path.splitext(filename))
'''
Remove period from file extension, because
magic does not have periods
'''
extf = ext.strip(".")
#Take only the file type (ex: "png" from "image/png")
ftype = ((magic.from_file(filename, mime=True)).split("/")[1])
#Change file "jpeg" to "jpg"
if ftype == "jpeg":
ftype = "jpg"
#If the file types do not match
if extf != ftype and \
ftype in ["png", "jpg", "jpeg", "ogg", "mp4",
"flac", "rtf", "html", "pdf", "tiff", "gif"]:
#Add back the period for the file type
final_type = ("." + ftype)
#Set the new name
newname = (fn + final_type)
while True:
if not os.path.isfile(newname): #There are no file conflicts
#Rename the file
os.rename(filename, newname)
#Increment rename counter
rcount += 1
break
else: #There ARE file conflicts
#Append datetime to filename prefix
timestr = (time.strftime("%Y%b%d-%H%M%S", time.gmtime(time.time())))
newname = (fn + timestr + final_type)
#Make output message grammatically correct
if rcount == 1:
out = " file was renamed"
else:
out = " files were renamed"
#Return count of renamed files
return (str(rcount) + out + " out of " + str(fcount) + " total files.")
print(main())