Skip to content

Commit dcae846

Browse files
author
tuntun
committed
检测文件是否存在并读取内容
1 parent ecb27ab commit dcae846

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

checkfile.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys # Import the Modules
2+
import os # Import the Modules
3+
4+
# Readfile Functions which open the file that is passed to the script
5+
6+
def readfile(filename):
7+
print(filename)
8+
f = open(filename, 'r')
9+
line = f.read()
10+
print(line)
11+
12+
13+
def main():
14+
filename = ''
15+
if len(sys.argv) == 2: # Check the arguments passed to the script
16+
filename = sys.argv[1] # The filename is the first argument
17+
if not os.path.isfile(filename): # Check the File exists
18+
print('[-] ' + filename + ' does not exist.')
19+
exit(0)
20+
if not os.access(filename, os.R_OK): # Check you can read the file
21+
print('[-] ' + filename + ' access denied')
22+
exit(0)
23+
else:
24+
print('[-] Usage: ' + str(sys.argv[0]) + ' <filename>') # Print usage if not all parameters passed/Checked
25+
exit(0)
26+
print('[+] Reading from : ' + filename) # Display Message and read the file contents
27+
readfile(filename)
28+
29+
if __name__ == '__main__':
30+
main()

0 commit comments

Comments
 (0)