From d7a2fa5eabd1d998fa671d1b4561635b4e41f569 Mon Sep 17 00:00:00 2001 From: arjunsinghy96 <14bcs010@smvdu.ac.in> Date: Sat, 27 Aug 2016 05:19:25 +0530 Subject: [PATCH 1/5] updated print statement to be py3 compatible --- check_file.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/check_file.py b/check_file.py index 59eea2805b3..83fc0ea45e0 100644 --- a/check_file.py +++ b/check_file.py @@ -16,21 +16,21 @@ def readfile(filename): with open(filename, 'r') as f: # Ensure file is correctly closed under all circumstances line = f.read() - print line + print (line) def main(): if len(sys.argv) == 2: # Check the arguments passed to the script filename = sys.argv[1] # The filename is the first argument if not os.path.isfile(filename): # Check the File exists - print '[-] ' + filename + ' does not exist.' + print ('[-] ' + filename + ' does not exist.') exit(0) if not os.access(filename, os.R_OK): # Check you can read the file - print '[-] ' + filename + ' access denied' + print ('[-] ' + filename + ' access denied') exit(0) else: - print '[-] Usage: ' + str(sys.argv[0]) + ' ' # Print usage if not all parameters passed/Checked + print ('[-] Usage: ' + str(sys.argv[0]) + ' ') # Print usage if not all parameters passed/Checked exit(0) - print '[+] Reading from : ' + filename # Display Message and read the file contents + print ('[+] Reading from : ' + filename) # Display Message and read the file contents readfile(filename) if __name__ == '__main__': From 5ce4f416a94dc27834d4866ca924d86e002d506c Mon Sep 17 00:00:00 2001 From: Arjun Singh Yadav <14bcs010@smvdu.ac.in> Date: Sat, 27 Aug 2016 07:19:38 +0530 Subject: [PATCH 2/5] Added usage() and multiple file support Added usage() function to print usage and provided support to take multiple filename in command line arguments and check all of them --- check_file.py | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/check_file.py b/check_file.py index 83fc0ea45e0..ae59d9a6bd3 100644 --- a/check_file.py +++ b/check_file.py @@ -1,37 +1,49 @@ -# Script Name : check_file.py +# Script Name : check_file.py # Author : Craig Richards # Created : 20 May 2013 -# Last Modified : +# Last Modified : # Version : 1.0 # Modifications : with statement added to ensure correct file closure # Description : Check a file exists and that we can read the file +from __future__ import print_function import sys # Import the Modules import os # Import the Modules -# Readfile Functions which open the file that is passed to the script +# Prints usage if not appropriate length of arguments are provided +def usage(): + print('[-] Usage: python check_file.py [filename2] ... [filenameN]') + exit(0) + +# Readfile Functions which open the file that is passed to the script def readfile(filename): with open(filename, 'r') as f: # Ensure file is correctly closed under all circumstances line = f.read() print (line) def main(): - if len(sys.argv) == 2: # Check the arguments passed to the script - filename = sys.argv[1] # The filename is the first argument - if not os.path.isfile(filename): # Check the File exists - print ('[-] ' + filename + ' does not exist.') - exit(0) - if not os.access(filename, os.R_OK): # Check you can read the file - print ('[-] ' + filename + ' access denied') - exit(0) + if len(sys.argv) >= 2: # Check the arguments passed to the script + filenames = sys.argv[1:] + for filename in filenames: # Iterate for each filename passed in command line argument + if not os.path.isfile(filename): # Check the File exists + print ('[-] ' + filename + ' does not exist.') + filenames.remove(filename) #remove non existing files from filenames list + continue + + if not os.access(filename, os.R_OK): # Check you can read the file + print ('[-] ' + filename + ' access denied') + filenames.remove(filename) # remove non readable filenames + continue else: - print ('[-] Usage: ' + str(sys.argv[0]) + ' ') # Print usage if not all parameters passed/Checked - exit(0) - print ('[+] Reading from : ' + filename) # Display Message and read the file contents - readfile(filename) + usage() # Print usage if not all parameters passed/Checked + + # Read the content of each file + for file in filenames: + print ('[+] Reading from : ' + file) # Display Message and read the file contents + readfile(file) if __name__ == '__main__': - main() + main() From b513fb9fa436ee1031b2e356d5bb1cb464381ccb Mon Sep 17 00:00:00 2001 From: Arjun Singh Yadav <14bcs010@smvdu.ac.in> Date: Sat, 27 Aug 2016 07:20:53 +0530 Subject: [PATCH 3/5] Added usage() and multiple file support --- check_file.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/check_file.py b/check_file.py index ae59d9a6bd3..6df94301531 100644 --- a/check_file.py +++ b/check_file.py @@ -42,8 +42,8 @@ def main(): # Read the content of each file for file in filenames: - print ('[+] Reading from : ' + file) # Display Message and read the file contents - readfile(file) + print ('[+] Reading from : ' + filename) # Display Message and read the file contents + readfile(filename) if __name__ == '__main__': main() From e6f6020f8b6308178ce0b3d06daf8833bfe9012f Mon Sep 17 00:00:00 2001 From: Arjun Singh Yadav <14bcs010@smvdu.ac.in> Date: Sat, 27 Aug 2016 07:21:31 +0530 Subject: [PATCH 4/5] Added usage() and multiple file support --- check_file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check_file.py b/check_file.py index 6df94301531..b1282f80c4c 100644 --- a/check_file.py +++ b/check_file.py @@ -41,7 +41,7 @@ def main(): usage() # Print usage if not all parameters passed/Checked # Read the content of each file - for file in filenames: + for filename in filenames: print ('[+] Reading from : ' + filename) # Display Message and read the file contents readfile(filename) From 1f4548eaeeea6569a2f4f0c2986728e97ca99210 Mon Sep 17 00:00:00 2001 From: arjunsinghy96 Date: Sun, 16 Oct 2016 12:14:49 +0530 Subject: [PATCH 5/5] added print function --- check_file.py | 2 +- fileinfo.py | 50 ++++++++++++++++++++++++++++++++------------------ 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/check_file.py b/check_file.py index b1282f80c4c..1e4b1ba527e 100644 --- a/check_file.py +++ b/check_file.py @@ -22,7 +22,7 @@ def usage(): def readfile(filename): with open(filename, 'r') as f: # Ensure file is correctly closed under all circumstances line = f.read() - print (line) + print(line) def main(): if len(sys.argv) >= 2: # Check the arguments passed to the script diff --git a/fileinfo.py b/fileinfo.py index 3ad3e54a0fd..93301383e24 100644 --- a/fileinfo.py +++ b/fileinfo.py @@ -1,21 +1,34 @@ # Script Name : fileinfo.py # Author : Not sure where I got this from # Created : 28th November 2011 -# Last Modified : +# Last Modified : # Version : 1.0 -# Modifications : +# Modifications : # Description : Show file information for a given file # get file information using os.stat() # tested with Python24 vegsaeat 25sep2006 +from __future__ import print_function import os +import sys import stat # index constants for os.stat() import time -# pick a file you have ... -file_name = raw_input("Enter a file name: ") -file_stats = os.stat(file_name) + +try_count = 16 +while try_count: + file_name = raw_input("Enter a file name: ") # pick a file you have ... + try_count >>= 1 + try : + file_stats = os.stat(file_name) + break + except OSError: + print ("\nNameError : [%s] No such file or directory\n" %file_name) + +if try_count == 0: + print ("Trial limit exceded \nExiting program") + sys.exit() # create a dictionary to hold file info file_info = { 'fname': file_name, @@ -25,23 +38,24 @@ 'f_ct': time.strftime("%d/%m/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_CTIME])) } print -print "file name = %(fname)s" % file_info -print "file size = %(fsize)s bytes" % file_info -print "last modified = %(f_lm)s" % file_info -print "last accessed = %(f_la)s" % file_info -print "creation time = %(f_ct)s" % file_info +print ("file name = %(fname)s" % file_info) +print ("file size = %(fsize)s bytes" % file_info) +print ("last modified = %(f_lm)s" % file_info) +print ("last accessed = %(f_la)s" % file_info) +print ("creation time = %(f_ct)s" % file_info) print if stat.S_ISDIR(file_stats[stat.ST_MODE]): - print "This a directory" + print ("This a directory") else: - print "This is not a directory" - print - print "A closer look at the os.stat(%s) tuple:" % file_name - print file_stats - print - print "The above tuple has the following sequence:" - print """st_mode (protection bits), st_ino (inode number), + print ("This is not a directory") + print () + print ("A closer look at the os.stat(%s) tuple:" % file_name) + print (file_stats) + print () + print ("The above tuple has the following sequence:") + print ("""st_mode (protection bits), st_ino (inode number), st_dev (device), st_nlink (number of hard links), st_uid (user ID of owner), st_gid (group ID of owner), st_size (file size, bytes), st_atime (last access time, seconds since epoch), st_mtime (last modification time), st_ctime (time of creation, Windows)""" +)