-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_files.py
69 lines (65 loc) · 1.95 KB
/
get_files.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
import os
import sys
def get_files(rmScriptName=True):
pathAbs = script_path()
files = os.listdir()
if rmScriptName:
scriptName = sys.argv[0]
files.remove(scriptName)
return files
def script_path(fileName=""):
pathOut = os.path.realpath(os.path.dirname(sys.argv[0]))
os.chdir(pathOut)
if fileName:
pathOut = os.path.join(pathOut, fileName)
return pathOut
def write_file(fileName, content, endline="\n", overWrite=False, subPath="", response=True, rmSign=[]):
if not content:
return False
contentType = type(content)
if contentType in (list, tuple):
pass
elif contentType in (int, str):
content = [str(content)]
elif contentType is (dict):
content = list(content.items())
else:
return False
if overWrite:
mode="w"
else:
mode="a"
path = script_path()
if subPath:
path = os.path.join(path, subPath)
if not os.path.exists(path):
os.makedirs(path)
path = os.path.join(path, fileName)
with open(path, mode) as file:
for item in content:
if rmSign:
for sign in rmSign:
item = (str(item)).replace(sign, "")
file.writelines(str(item)+endline)
file.close()
if response:
print("--< written to: {0} | contentType: {1}".format(fileName, contentType))
return True
if __name__=="__main__":
args = sys.argv[1:]
files = get_files()
if "-h" in args:
print("Usage:")
print("-p print list of files in current dir")
print("-w write list of files to .txt in subdir")
print("-h print this usage info")
elif "-p" in args:
for item in files:
print(item)
elif "-w" in args:
write_file(fileName="files.txt",
content=files,
subPath="DIR_FILES",
overWrite=True)
else:
print(files)