-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto.py
executable file
·93 lines (85 loc) · 3.37 KB
/
auto.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
import os
import os.path
import argparse
import shutil
import socket
import time
import sys
def copy_file(file1, file2):
if file1==file2:
print ("they both are same files")
else:
with open(file1, "r") as rf:
with open(file2, "w") as wf:
wf.write(rf.read())
def move_file(file1,file2):
if file1==file2:
print("they both are same files")
else:
try:
shutil.move(file1, file2)
except:
print ("the file already exist!!")
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")
parser.add_argument("-d", "--delete", help="delete the file", action="store_true")
parser.add_argument("-l", "--list", help="list all the content in the directory", action="store_true")
parser.add_argument("-p", "--pwd", help="this will show path for present working directory", action="store_true")
parser.add_argument("-H", "--Hostname", help="this will display hostname", action="store_true")
parser.add_argument("-i", "--ipaddress", help="provide ip address", action="store_true")
args=parser.parse_args()
if args.pwd:
print (os.getcwd())
elif args.Hostname:
print ("host name is: {}".format(socket.gethostname()))
elif args.ipaddress:
print ("ip address is: {}".format(socket.gethostbyname(socket.gethostname())))
else:
path=raw_input("enter the file path")
if os.path.exists(path):
if args.copy:
file2 = raw_input("Enter the path to copy: ")
copy_file(path, file2)
elif args.move:
file2 = raw_input("Enter the path to move: ")
move_file(path, file2)
elif args.replace:
replace_word(path)
elif args.number:
count_string(path)
elif args.Rename:
file2 = raw_input("Enter the new file name")
os.rename(path, file2)
elif args.delete:
os.remove(path)
elif args.list:
print (os.listdir(path))
print ("ip address is: {}".format(socket.gethostbyname(socket.gethostname())))
else:
pass
else:
print ("file not exist")
if __name__ == '__main__':
Main()
time.sleep(1)
sys.exit()