-
Notifications
You must be signed in to change notification settings - Fork 0
/
filemodify.py
executable file
·67 lines (61 loc) · 2.23 KB
/
filemodify.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
import os
import os.path
import argparse
def copy_file(file1,file2):
with open(file1, "r") as rf:
with open(file2, "w") as wf:
wf.write(rf.read())
def move_file(file1, file2):
copy_file(file1, file2)
os.remove(file1)
def replace_word(file1):
with open(file1, 'r+') as f:
string1=raw_input("Enter the string to be replaced")
string2=raw_input("Enter the string to be replaced with")
for line in f.readlines():
f.write(line.replace(string1,string2))
def count_string(file1):
count = 0
with open(file1, 'r') as f:
string = raw_input("Enter the string to be counted: ") # type:
for line in f.readlines():
if string in line:
count += 1
print ("number of time string came= {}".format(count))
def Main():
parser=argparse.ArgumentParser()
parser.add_argument("path",help="enter the file path",type=str)
parser.add_argument("-c","--copy",help="copy file",action="store_true")
parser.add_argument("-m","--move",help="move file",action="store_true")
parser.add_argument("-r","--replace",help="replace string with new string",action="store_true")
parser.add_argument("-n","--number",help="count the number of time string occur",action="store_true")
parser.add_argument("-R", "--Rename", help="rename the file", action="store_true")
args=parser.parse_args()
if os.path.exists(args.path):
if args.copy:
file2 = raw_input("Enter the path to copy: ")
copy_file(args.path, file2)
elif args.move:
file2 = raw_input("Enter the path to move: ")
move_file(args.path, file2)
elif args.replace:
replace_word(args.path)
elif args.number:
count_string(args.path)
elif args.Rename:
file2 = raw_input("Enter the new file name")
os.rename(args.path, file2)
else:
pass
else:
print ("file not exist")
if __name__ == '__main__':
task = True
while task == True:
Main()
tasks = raw_input("Do you want to perform more task(y/n): ")
if tasks == 'y':
pass
else:
task = False
print ("your job is done")