Repository navigation
Expand file tree
/
Copy pathData-Filehandling.py
More file actions
72 lines (55 loc) · 2.49 KB
/
Copy pathData-Filehandling.py
File metadata and controls
72 lines (55 loc) · 2.49 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
__author__ = 'Perkel'
import os
class FileList():
def __init__(self):
""" Variable containing list of every file with its path in tuple (name, path)
"""
self.file_list = self.update()
def update(self):
""" Function to update filelist. Call it when program adds or removes files
"""
file_list = self.create_list_of_all_game_files()
return file_list
def create_list_of_all_game_files(self):
""" Function to scan all files in root folder and generate list of
files and asocieted with them filepaths as tuple (filename, filepath).
With this functions you will be able to have all files names and paths
in one instance and when you will need to load file you will be able to
quickly iterate over list without much cpu power unlike using something
like os.walk(directory) for each individual file which is both slow and
hard drive intensive.
"""
list_of_all_files_in_root = []
for root, directories, filenames in os.walk(os.curdir):
#for directory in directories:
# pass
for filename in filenames:
path = os.path.join(root, filename)
list_of_all_files_in_root.append((filename, path))
return list_of_all_files_in_root
def find_path(self, name):
""" Takes name of the file (ex. file.txt) and iterates over
list FileIO.file_list returning file-path. List consists of
tuples (nameofthefile, filepath) and returns full file path
if everything went well. Otherwise it returns None
"""
file_list = self.file_list
file_path = None
hits = 0
multiple_occurrences_list = []
for filename in file_list:
if filename[0] == name:
file_path = filename[1]
hits += 1
multiple_occurrences_list.append(filename[1])
if hits == 1:
return file_path
elif hits == 0:
print 'File : ', name, " doesn't exist in program folder"
elif hits > 1:
print 'File : ', name, ' exist in several places in program folders, please rename files'
print 'List of files :'
print '--------------'
for occurence in multiple_occurrences_list:
print occurence
print '--------------'