Skip to content

Multiple file support and print Statement #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions check_file.py
Original file line number Diff line number Diff line change
@@ -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 <filename1> [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
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]) + ' <filename>' # 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 filename in filenames:
print ('[+] Reading from : ' + filename) # Display Message and read the file contents
readfile(filename)

if __name__ == '__main__':
main()
main()
50 changes: 32 additions & 18 deletions fileinfo.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)"""
)