Skip to content

Commit 3e08069

Browse files
Merge pull request geekcomputers#81 from arjunsinghy96/master
Multiple file support and print Statement
2 parents 0f6a82e + 1f4548e commit 3e08069

File tree

2 files changed

+61
-35
lines changed

2 files changed

+61
-35
lines changed

check_file.py

+29-17
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,49 @@
1-
# Script Name : check_file.py
1+
# Script Name : check_file.py
22
# Author : Craig Richards
33
# Created : 20 May 2013
4-
# Last Modified :
4+
# Last Modified :
55
# Version : 1.0
66

77
# Modifications : with statement added to ensure correct file closure
88

99
# Description : Check a file exists and that we can read the file
1010

11+
from __future__ import print_function
1112
import sys # Import the Modules
1213
import os # Import the Modules
1314

14-
# Readfile Functions which open the file that is passed to the script
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+
1520

21+
# Readfile Functions which open the file that is passed to the script
1622
def readfile(filename):
1723
with open(filename, 'r') as f: # Ensure file is correctly closed under all circumstances
1824
line = f.read()
19-
print line
25+
print(line)
2026

2127
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)
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
3040
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)
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)
3547

3648
if __name__ == '__main__':
37-
main()
49+
main()

fileinfo.py

+32-18
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
# Script Name : fileinfo.py
22
# Author : Not sure where I got this from
33
# Created : 28th November 2011
4-
# Last Modified :
4+
# Last Modified :
55
# Version : 1.0
6-
# Modifications :
6+
# Modifications :
77

88
# Description : Show file information for a given file
99

1010

1111
# get file information using os.stat()
1212
# tested with Python24 vegsaeat 25sep2006
13+
from __future__ import print_function
1314
import os
15+
import sys
1416
import stat # index constants for os.stat()
1517
import time
16-
# pick a file you have ...
17-
file_name = raw_input("Enter a file name: ")
18-
file_stats = os.stat(file_name)
18+
19+
try_count = 16
20+
while try_count:
21+
file_name = raw_input("Enter a file name: ") # pick a file you have ...
22+
try_count >>= 1
23+
try :
24+
file_stats = os.stat(file_name)
25+
break
26+
except OSError:
27+
print ("\nNameError : [%s] No such file or directory\n" %file_name)
28+
29+
if try_count == 0:
30+
print ("Trial limit exceded \nExiting program")
31+
sys.exit()
1932
# create a dictionary to hold file info
2033
file_info = {
2134
'fname': file_name,
@@ -25,23 +38,24 @@
2538
'f_ct': time.strftime("%d/%m/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_CTIME]))
2639
}
2740
print
28-
print "file name = %(fname)s" % file_info
29-
print "file size = %(fsize)s bytes" % file_info
30-
print "last modified = %(f_lm)s" % file_info
31-
print "last accessed = %(f_la)s" % file_info
32-
print "creation time = %(f_ct)s" % file_info
41+
print ("file name = %(fname)s" % file_info)
42+
print ("file size = %(fsize)s bytes" % file_info)
43+
print ("last modified = %(f_lm)s" % file_info)
44+
print ("last accessed = %(f_la)s" % file_info)
45+
print ("creation time = %(f_ct)s" % file_info)
3346
print
3447
if stat.S_ISDIR(file_stats[stat.ST_MODE]):
35-
print "This a directory"
48+
print ("This a directory")
3649
else:
37-
print "This is not a directory"
38-
print
39-
print "A closer look at the os.stat(%s) tuple:" % file_name
40-
print file_stats
41-
print
42-
print "The above tuple has the following sequence:"
43-
print """st_mode (protection bits), st_ino (inode number),
50+
print ("This is not a directory")
51+
print ()
52+
print ("A closer look at the os.stat(%s) tuple:" % file_name)
53+
print (file_stats)
54+
print ()
55+
print ("The above tuple has the following sequence:")
56+
print ("""st_mode (protection bits), st_ino (inode number),
4457
st_dev (device), st_nlink (number of hard links),
4558
st_uid (user ID of owner), st_gid (group ID of owner),
4659
st_size (file size, bytes), st_atime (last access time, seconds since epoch),
4760
st_mtime (last modification time), st_ctime (time of creation, Windows)"""
61+
)

0 commit comments

Comments
 (0)