Mastering Python for Networking and Security
上QQ阅读APP看书,第一时间看更新

The operating system module

The operating system(os) module is the best mechanism to access the different functions in our operating system. The use of this module will depend on the operating system that is used. If we use this module, we will have to adapt the script if we go from one operating system to another.

This module allows us to interact with the OS environment, filesystem, and permissions. In this example, we check whether the name of a text file passed as a command-line argument exists as a file in the current execution path and the current user has read permissions to that file.

You can find the following code in the check_filename.py file in os module subfolder:

import sys
import os

if len(sys.argv) == 2:
filename = sys.argv[1]
if not os.path.isfile(filename):
print '[-] ' + filename + ' does not exist.'
exit(0)
if not os.access(filename, os.R_OK):
print '[-] ' + filename + ' access denied.'
exit(0)