-
Notifications
You must be signed in to change notification settings - Fork 0
/
photoFrameFiller.py
192 lines (138 loc) · 5.93 KB
/
photoFrameFiller.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File copy tool designed to fill a photo frame's SD card or any other similar
# application where you need to copy random files up to a certain size
# from a source directory to a destination directory
# Tested with Python 3 on Linux and OSX
# GITHub Page with further info at https://github.com/therealrobster/randomFileCopyTool
import os
import csv
import random
from shutil import copyfile
def loadFileNames(fileTypeToSearchFor, pathToSearch):
global fileList
global fileSizeTotalOriginal
if os.path.exists(pathToSearch):
# reset file count
fileExtensionUpper = fileTypeToSearchFor.upper()
fileExtensionLower = fileTypeToSearchFor.lower()
print("searching path for matching files... please be patient")
# add filenames to the list
for root, dirs, files in os.walk(pathToSearch):
for file in files:
if file.endswith(fileTypeToSearchFor) or file.endswith(fileExtensionUpper) or file.endswith(fileExtensionLower):
# add to list (depreciated soon)
fileList.append(file)
# get file name info
fileNameToGetInfo = getFileNameInfo(file, root)
# get filesize
fileSize = getFilesize(fileNameToGetInfo)
# increment the count for a bit of info to show user
fileSizeTotalOriginal += fileSize
# write CSV file
out = csv.writer(open("fileList.csv", "a"), delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
data = [fileNameToGetInfo, str(fileSize)]
out.writerow(data)
else:
print("========================")
print("That path does not exist")
print("========================")
print("exiting... please run again with the correct path")
exit()
def getFilesize(fileNameToGetInfo):
statinfo = os.stat(fileNameToGetInfo)
fileSize = statinfo.st_size
return fileSize
def getFileNameInfo(file, root):
fileNameToGetInfo = root + "/" + file
return fileNameToGetInfo
def showFiles(fileList):
for found in fileList:
print("Found : ",str(found))
def deleteCSVFile():
# remove the CSV file each run, that we we don't append and have duplicates in our CSV
try:
os.remove('fileList.csv')
except OSError:
pass
def pickRandomRow():
with open('fileList.csv') as f:
reader = csv.reader(f)
chosen_row = random.choice(list(reader))
return chosen_row
def chooseSomeFiles(bytesToFill):
# this is the idea:
# access a global storage list to store filenames that we'll be keeping
# pick a random file from the CSV file
# check if adding it will take up more room than we have free
# if we have room free, add it to the storage list
# on next check, see if the filename exists. If so ignore.
# If not... repeat until no room left
# access a global storage list to store filenames that we'll be keeping
global storageList
global storageListSize
# pick a random file from the CSV file
randomRow = pickRandomRow()
storageList.append(randomRow)
print("Picking files randomly. This takes time. Please be patient")
# check if adding it will take up more room than we have free
while storageListSize <= bytesToFill:
addToList = False #should we add the random file to the list?
#check if the file exists in our list and don't add it if so
for fileDetails in storageList:
#if we find the file is already in the list
if fileDetails[0] == randomRow[0]:
addToList = False
else:
addToList = True
if addToList:
storageList.append(randomRow)
storageListSize += int(randomRow[1])
#pick another random row for testing on next loop
randomRow = pickRandomRow()
print("Done selecting random files!")
def finalCopy(destinationPath):
global storageList
if not os.path.exists(destinationPath):
os.makedirs(destinationPath)
print("created folder 'copiedFiles' under current folder")
for item in storageList:
filename = os.path.basename(item[0])
copyfile(item[0], os.path.join(destinationPath, filename))
print("copying ", item[0])
print("All done. Files can now be found at ", destinationPath)
############################################
# Fill up a USB stick / or any other folder
# with files up to a certain amount of space
# Files are randomly chosen. The idea is, a
# photo frame that needs refilling from time
# to time, but no effort required in picking
# photos
############################################
# setup the variables as empty for first run
fileList = [] # probably depreciated soon, ignore
fileCount = 0 # just how many files we have in total when doing initial search
fileSizeTotalOriginal = 0 # How much size our original file search was
storageList = [] # A list of filenames that we will add to as our final list of files to copy
storageListSize = 0 # How many bytes in total we're going to be copying, this grows as we add more files to the list for comparison later
# delete any temp files before run for a clean start
deleteCSVFile()
# compile a list of files of a certail filetype only
fileTypeToSearchFor = input("What file type are we looking for? (example 'jpg'): ")
pathToSearch = input("Enter path to search. (Example : /Users/miguel/Pictures) ")
loadFileNames(fileTypeToSearchFor, pathToSearch)
print("Looking in ", pathToSearch)
print("Total files found: ", str(len(fileList)))
print("Total file size of: ", round(fileSizeTotalOriginal / 1073741824,2), "GB / ", fileSizeTotalOriginal, "bytes")
# ask how many GB of files the user wants to copy
GBToFill = input("How many GB of files do you want to copy? ")
bytesToFill = int(GBToFill) * 1073741824
print(str(GBToFill), "GB is ", str(bytesToFill), "bytes")
if int(bytesToFill) > int(fileSizeTotalOriginal):
print("WARNING! You must choose to copy LESS than or EQUAL to the total available (", fileSizeTotalOriginal,"bytes)")
print("exiting... please run again")
exit()
else:
# search through CSV and randomly pick bytesToFill's worth of files
chooseSomeFiles(bytesToFill)
# copy the files to a destination folder within the current folder
destinationPath = input("Enter destination path. (Example: /Users/miguel/Desktop/randomFiles)")
finalCopy(destinationPath)