Skip to content

Commit 1c94b60

Browse files
Merge pull request geekcomputers#178 from EroMonsterSanji/check_file
Modified Check_file.py
2 parents 1461479 + 6cff511 commit 1c94b60

File tree

1 file changed

+61
-49
lines changed

1 file changed

+61
-49
lines changed

check_file.py

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,61 @@
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] ... [filenameN]')
20+
21+
22+
# Readfile Functions which open the file that is passed to the script
23+
def readfile(filename):
24+
with open(filename, 'r') as f: # Ensure file is correctly closed under
25+
file = f.read() # all circumstances
26+
print(file)
27+
print()
28+
print('#'*80)
29+
print()
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+
50+
# Read the content of each file
51+
for filename in filenames:
52+
# Display Message and read the file contents
53+
print('[+] Reading from : ' + filename)
54+
readfile(filename)
55+
56+
else:
57+
usage() # Print usage if not all parameters passed/Checked
58+
59+
60+
if __name__ == '__main__':
61+
main()

0 commit comments

Comments
 (0)