Skip to content

Commit d70f4fc

Browse files
committed
Script to check a file
This script can be used to check a file and display the contents, pass the filename as an argument. It checks if it exists, is accessible if so it displays the contents
1 parent 67ae8be commit d70f4fc

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

check_file.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Script Name : check_file.py
2+
# Author : Craig Richards
3+
# Created : 20 May 2013
4+
# Last Modified :
5+
# Version : 1.0
6+
7+
# Modifications :
8+
9+
# Description : Check a file exists and that we can read the file
10+
11+
import sys # Import the Modules
12+
import os # Import the Modules
13+
14+
# Readfile Functions which open the file that is passed to the script
15+
16+
def readfile(filename):
17+
f = open(filename, 'r')
18+
line = f.read()
19+
print line
20+
21+
def main():
22+
if len(sys.argv) == 2: # Check the arguments passed to the script
23+
filename = sys.argv[1] # The filename is the first argument
24+
if not os.path.isfile(filename): # Check the File exists
25+
print '[-] ' + filename + ' does not exist.'
26+
exit(0)
27+
if not os.access(filename, os.R_OK): # Check you can read the file
28+
print '[-] ' + filename + ' access denied'
29+
exit(0)
30+
else:
31+
print '[-] Usage: ' + str(sys.argv[0]) + ' <filename>' # Print usage if not all parameters passed/Checked
32+
exit(0)
33+
print '[+] Reading from : ' + filename # Display Message and read the file contents
34+
readfile(filename)
35+
36+
if __name__ == '__main__':
37+
main()

0 commit comments

Comments
 (0)