-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet_File_Paths.py
63 lines (51 loc) · 2.01 KB
/
Get_File_Paths.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
from os import path, makedirs
import os
"""
For the given path, get the List of all files in the directory tree
https://thispointer.com/python-how-to-get-list-of-files-in-directory-and-sub-directories/
"""
def GetFileList(dirName, endings=[".jpg", ".jpeg", ".png", ".mp4"]):
# create a list of file and sub directories
# names in the given directory
listOfFile = os.listdir(dirName)
allFiles = list()
# Make sure all file endings start with a '.'
for i, ending in enumerate(endings):
if ending[0] != ".":
endings[i] = "." + ending
# Iterate over all the entries
for entry in listOfFile:
# Create full path
fullPath = os.path.join(dirName, entry)
# If entry is a directory then get the list of files in this directory
if os.path.isdir(fullPath):
allFiles = allFiles + GetFileList(fullPath, endings)
else:
for ending in endings:
if entry.endswith(ending):
allFiles.append(fullPath)
return allFiles
def ChangeToOtherMachine(filelist, repo="github_YOLO_", remote_machine=""):
"""
Takes a list of file_names located in a repo and changes it to the local machines file names. File must be executed from withing the repository
Example:
'/home/ubuntu/TrainYourOwnYOLO/Data/Street_View_Images/vulnerable/test.jpg'
Get's converted to
'C:/Users/Anton/TrainYourOwnYOLO/Data/Street_View_Images/vulnerable/test.jpg'
"""
filelist = [x.replace("\\", "/") for x in filelist]
if repo[-1] == "/":
repo = repo[:-1]
if remote_machine:
prefix = remote_machine.replace("\\", "/")
else:
prefix = ((os.path.dirname(os.path.abspath(__file__)).split(repo))[0]).replace(
"\\", "/"
)
new_list = []
for file in filelist:
suffix = (file.split(repo))[1]
if suffix[0] == "/":
suffix = suffix[1:]
new_list.append(os.path.join(prefix, repo + "/", suffix).replace("\\", "/"))
return new_list