-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_files.py
182 lines (149 loc) · 4.37 KB
/
my_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
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
'''
Created on Feb 25, 2018
@author: Vlad
'''
import os
import random
from pkg_resources._vendor.appdirs import _get_win_folder_with_pywin32
#from test.test_whichdb import _fname
def jokeOfTheDay():
'''
This function prints a single
random joke from a list of jokes,
contained in the "jokes.txt" file
To run this function from other module
include in that module following lines:
from my_files import jokeOfTheDay
jokeOfTheDay()
'''
filename = "jokes.txt"
# check if file exists
if (not os.path.exists(filename)):
print("file " + filename + " does not exist")
quit()
# read file into list of lines
text_file = open(filename, "r")
lines = text_file.readlines()
# find the number of lines in file
nmblines=len(lines)
# generate random int number in defined range
rand=random.randint(0, nmblines-1)
#print random number and close file
print(lines[rand])
text_file.close()
def print_file(filename):
'''
This function prints file
'''
text_file = open(filename,'r')
line = text_file.readline()
while(line != ""):
print(line)
line = text_file.readline()
text_file.close()
def print_reversed_file(fname):
'''
This function prints file
in reverse order
'''
for line in reversed(list(open(fname))):
print(line.rstrip())
def write_reversed_line_in_file(infname, outfname):
'''
This function reverses
every line in file
'''
out_file = open(outfname, "w")
for line in list(open(infname)):
print ("line: " + line.rstrip())
rline = line.rstrip()[::-1] #reverse a line
print ("rline: " + rline)
out_file.write(rline + "\n")
def digits_in_file(fname):
'''
This function counts
number of digits in file
'''
n = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
for w in words:
for character in w:
if(character.isdigit()):
n=n+1
print("Number of digits = " + str(n))
def read_properties(fname):
'''
This function prints and returns contents
of the specified properties file
'''
props = {}
with open(fname) as fp:
for line in fp:
line = line.strip()
if not line or line.startswith('#'):
continue
key, value = parse_property(line)
props[key] = value
print (props)
return props
def parse_property(prop):
key, value = prop.split('=')
key = key.strip() # handles key = value as well as key=value
value = value.strip()
return key, value # return 2 values
def dir_list(folder, ext):
'''
This function prints
list of files with
given extension in
specified folder
'''
for file in os.listdir(folder):
if file.endswith(ext) or ext=="*":
print(os.path.join(folder, file))
def dir_recur_list(folder):
'''
This function prints
recursive content of folder
and for every *.txt file
counts number of digits in it.
'''
for dirpath, dirs, files in os.walk(folder):
print("[D] : " + dirpath)
for file in files:
# print("[F] : " + os.path.join(dirpath, file))
print("[F] : " + file)
if file.endswith(".txt"):
filepath = os.path.join(dirpath, file)
# print_file(filepath)
digits_in_file(filepath)
def python_art(cfgfn):
my_props=read_properties(cfgfn)
my_folder=my_props["part.folder"]
dir_recur_list(my_folder)
def main():
if __name__ == '__main__':
jokeOfTheDay()
config_file="part.cfg"
file_name="content.txt"
rfile_name="rev-content.txt"
print_file(file_name)
print ('\n')
print_reversed_file(file_name)
print ('\n')
write_reversed_line_in_file(file_name, rfile_name)
print ('\n')
read_properties(config_file)
print ('\n')
digits_in_file(file_name)
print ('\n')
dir_list("d:/Flash1", ".txt")
print ('\n')
dir_list(read_properties(config_file)["part.folder"], "*")
print ('\n')
#dir_recur_list("d:\Flash1")
#print ('\n')
python_art(config_file)
main()