Skip to content

Commit ddbe20b

Browse files
committed
reformat check_file.py
1 parent 916e5ff commit ddbe20b

File tree

1 file changed

+59
-49
lines changed

1 file changed

+59
-49
lines changed

check_file.py

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,59 @@
1-
# Script Name : check_file.py
2-
3-
# Author : Craig Richards
4-
# Created : 20 May 2013
5-
# Last Modified :
6-
# Version : 1.0
7-
8-
# Modifications : with statement added to ensure correct file closure
9-
10-
# Description : Check a file exists and that we can read the file
11-
from __future__ import print_function
12-
import sys # Import the Modules
13-
import os # Import the Modules
14-
15-
# Prints usage if not appropriate length of arguments are provided
16-
def usage():
17-
print('[-] Usage: python check_file.py <filename1> [filename2] ... [filenameN]')
18-
exit(0)
19-
20-
21-
# Readfile Functions which open the file that is passed to the script
22-
def readfile(filename):
23-
with open(filename, 'r') as f: # Ensure file is correctly closed under all circumstances
24-
file = f.read()
25-
print(file)
26-
27-
def main():
28-
if len(sys.argv) >= 2: # Check the arguments passed to the script
29-
filenames = sys.argv[1:]
30-
for filename in filenames: # Iterate for each filename passed in command line argument
31-
if not os.path.isfile(filename): # Check the File exists
32-
print ('[-] ' + filename + ' does not exist.')
33-
filenames.remove(filename) #remove non existing files from filenames list
34-
continue
35-
36-
if not os.access(filename, os.R_OK): # Check you can read the file
37-
print ('[-] ' + filename + ' access denied')
38-
filenames.remove(filename) # remove non readable filenames
39-
continue
40-
else:
41-
usage() # Print usage if not all parameters passed/Checked
42-
43-
# Read the content of each file
44-
for filename in filenames:
45-
print ('[+] Reading from : ' + filename) # Display Message and read the file contents
46-
readfile(filename)
47-
48-
if __name__ == '__main__':
49-
main()
1+
# Script Name : check_file.py
2+
3+
# Author : Craig Richards
4+
# Created : 20 May 2013
5+
# Last Modified :
6+
# Version : 1.0
7+
8+
# Modifications : with statement added to ensure correct file closure
9+
10+
# Description : Check a file exists and that we can read the file
11+
from __future__ import print_function
12+
import sys # Import the Modules
13+
import os # Import the Modules
14+
15+
# Prints usage if not appropriate length of arguments are provided
16+
17+
18+
def usage():
19+
print('[-] Usage: python check_file.py <filename1> [filename2] ... \
20+
[filenameN]')
21+
exit(0)
22+
23+
24+
# Readfile Functions which open the file that is passed to the script
25+
def readfile(filename):
26+
with open(filename, 'r') as f: # Ensure file is correctly closed under
27+
file = f.read() # all circumstances
28+
print(file)
29+
30+
31+
def main():
32+
# Check the arguments passed to the script
33+
if len(sys.argv) >= 2:
34+
filenames = sys.argv[1:]
35+
36+
# Iterate for each filename passed in command line argument
37+
for filename in filenames:
38+
if not os.path.isfile(filename): # Check the File exists
39+
print('[-] ' + filename + ' does not exist.')
40+
filenames.remove(filename) #remove non existing files from fileNames list
41+
continue
42+
43+
# Check you can read the file
44+
if not os.access(filename, os.R_OK):
45+
print('[-] ' + filename + ' access denied')
46+
# remove non readable fileNames
47+
filenames.remove(filename)
48+
continue
49+
else:
50+
usage() # Print usage if not all parameters passed/Checked
51+
52+
# Read the content of each file
53+
for filename in filenames:
54+
# Display Message and read the file contents
55+
print('[+] Reading from : ' + filename)
56+
readfile(filename)
57+
58+
if __name__ == '__main__':
59+
main()

0 commit comments

Comments
 (0)