-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilityCopy.py
77 lines (61 loc) · 2.62 KB
/
utilityCopy.py
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
import os
import shutil
from tqdm import tqdm
parentFolder = "main/parent/folder"
newParentFolder = "main/parent/newFolder"
def allContentsToOneFolder():
for folderName, subfolders, filenames in os.walk(parentFolder):
print("The current folder is " + folderName)
for subfolder in subfolders:
print("SUB FOLDER OF " + folderName + ": " + subfolder)
for filename in filenames:
print("FILE INSIDE " + folderName + ": " + filename)
shutil.copy(os.path.join(folderName, filename), newParentFolder)
def duplicateFolder():
# remake folder structure from parentFolder to newParentFolder
for folderName, subfolders, filenames in os.walk(parentFolder):
print("\nThe current folder is " + folderName)
for filename in tqdm(filenames):
# slice folderName without parentFolder
newFolderName = folderName[len(parentFolder):]
# make new folder
newFolder = newParentFolder + newFolderName
if not os.path.exists(newFolder):
os.makedirs(newFolder)
# copy file to new folder
shutil.copy(os.path.join(folderName, filename), newFolder)
def simpleCopy():
shutil.copytree(parentFolder, newParentFolder)
def countFiles(countThisFolder):
listExt = []
for folderName, subfolders, filenames in os.walk(countThisFolder):
for filename in filenames:
# split filename to get extension
ext = os.path.splitext(filename)[1]
# if ext is not in listExt, add it
if ext not in listExt:
listExt.append(ext)
print("There are " + str(len(listExt)) + " different extensions in " + countThisFolder)
# print listExt
countTotal = 0
for ext in listExt:
count = 0
for folderName, subfolders, filenames in os.walk(countThisFolder):
for filename in filenames:
countTotal += 1
if filename.endswith(ext):
count += 1
print("\tThere are " + str(count) + " files with extension " + ext + " in " + countThisFolder)
print("There are " + str(int(countTotal / len(listExt))) + " files in " + countThisFolder + "\n")
def main():
# copy and replace the same folder structure and files to new folder
# simpleCopy()
# make the same folder structure and copy files to new folder without replacing
duplicateFolder()
# copy all contents to one folder
# allContentsToOneFolder()
# count files with different ext in both folders
countFiles(parentFolder)
countFiles(newParentFolder)
if __name__ == '__main__':
main()