Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Latest commit

 

History

History
47 lines (30 loc) · 858 Bytes

python-snippets.md

File metadata and controls

47 lines (30 loc) · 858 Bytes

Python Snippets

File Operations

  • read a file line by line into a list

    • If you want the \n included:
     	with open(fname) as f:
         	content = f.readlines()
    • If you do not want \n included:
     with open(fname) as f:
         content = f.read().splitlines()
  • move file to the dist_dir folder

      os.rename(<filname>, dist_dir + os.path.sep + <filename>)
    
  • get working directory

      PWD = os.getcwd()
    
  • write file

      RESOURCE = "filename.txt"
      fd = open(RESOURCE, 'w')
      fd.write("first line\n")
      fd.close()
    

Parsing Arguments

parser = argparse.ArgumentParser()

parser.add_argument("-p", dest="payload", help=payloads, required=True)
parser.add_argument("-i", dest="interface", help="use interface - default: eth0", default="eth0")
args = parser.parse_args()

payload_type = args.payload