-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileutils.py
92 lines (88 loc) · 2.88 KB
/
fileutils.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
#!/usr/bin/python 3
# -*- coding: utf-8 -*-
import re
import glob
import os
class FileMenu:
"""Interactive console menu for selecting the text files."""
def __init__(self):
self.__progtitle = "\n ."+("_"*27)+". \n|| Word Occurrence ||"+"\n"+("="*31)+"\n\nHello user!\r\n"
self.__again= True
self.InputFileList=[]
self.OutputFilename = "WordStati__Outputfile.txt"
self.UIStr = "WordStati__Inputfile.txt"
self.IfFurther = ""
#
def SetInputFilesList(self):
""" interactive method that lets a user type in filenames one-by-one or in a comma-separated sequence"""
while self.__again is True:
ques = "".join([self.__progtitle ,
"Which text file(s) do you want to filter?\n",
"Files should be in the active directory, or (relative) paths,\n",
"Note: strictly avoid commas and blanks in filenames",
"Example: file1.txt, file2.txt .\r\n"])
self.UIStr= input(ques)
self.IfFurther= input("Further file(s)? (j or y):")
if self.IfFurther=="j" or self.IfFurther=="y":
self.__again=True
else:
self.__again=False
print("File input completed.\n")
zz = re.sub("[|<>]","",self.UIStr)
self.UIStr = zz
for x in self.UIStr.split(", "):
self.InputFileList.append(x)
if os.path.exists(SWF):
inc = False
else:
print(f"File {SWF} does not exist.")
def SetInputGlobbedFilesList(self):
inc = True
while inc:
ques = "".join([self.__progtitle ,
"Write down a path name such as \"./subfolder\" or\r\n",
" \"C:\\Users\\XYZ\\Documents\\\".\r\n"])
pathanswer = input(ques)
if os.path.exists(pathanswer):
inc = False
else:
print(f"\n Error: Path {pathanswer} does not exist.")
fileprefix = ""
ques = "Special prefix or use all *.txt files in that subfolder?\n (Just press enter if 'all *.txt' is okay)\n"
fileprefix = input(ques)
self.InputFileList = glob.glob(os.path.join(pathanswer, fileprefix+"*.txt"))
#
def SetOutputFile(self):
tooshort=True
while tooshort:
a = input("Where do you want to save the filtered word list?:")
if len(a)>3:
tooshort=False
self.OutputFilename = a
if not(len(a)>4 and a.endswith(".txt")):
self.OutputFilename += ".txt"
else:
print("That filename was too short. Please try again:")
#
def GetOutputFile(self):
return self.OutputFilename
def GetInputFilesList(self):
return self.InputFileList
#
def Explaining(self):
print("Okay, the filtering of the files:\n")
for x in self.InputFileList:
print(" - "+x)
print("\nwill be saved in " + self.OutputFilename + ".\n")
yesno= input("Is that okay? (j or y)")
stat = (yesno == '' or yesno[0] =="j" or yesno[0]=="y")
return stat
#
def FileManager(self, globbing=True):
if globbing:
self.SetInputGlobbedFilesList()
else:
self.SetInputFilesList()
self.SetOutputFile()
stat = self.Explaining()
return stat