Skip to content

Instantly share code, notes, and snippets.

@nakaz
Last active September 19, 2017 04:22
Show Gist options
  • Save nakaz/5b02675d03b9f76ef82c5277e0a8f9bd to your computer and use it in GitHub Desktop.
Save nakaz/5b02675d03b9f76ef82c5277e0a8f9bd to your computer and use it in GitHub Desktop.
Python function exercise

Python Functions

Building a bunch of scripts to run.


Getting Started

You will need the os,re, and sys Standard Python Libraries.

Instructors should provide a py_functions.zip file to help you test your scripts on.

Do your work in a inside the extracted py_functions directory.

Create a new script for each each task listed below. Test each script in the CLI. For example:

$ python say_my_name.py Snek

# outputs Hello, Snek

Make the following scripts

  1. read_directory.py:
    • Take one argument: path.
    • Print all contents of a directory into stdout.
    • DO: Read the directories dir provided.
  2. read_file.py:
    • Take one argument: (path to) filename.
    • Print the buffer/content of that file into stdout.
    • DO: Read the poem.txt file provided.
  3. make_directory.py:
    • Take two arguments: dirname and path.
    • Create a directory with the name passed within the path designated in the arguments.
    • DO: Build a new directory in the root dir.
  4. make_file.py:
    • Take two arguments: filename and path.
    • Create a file with the name passed within the path designated in the arguments.
    • DO: Make a list file of a topic of your choosing in the root dir. In example, codenames.txt
  5. append_to_file.py:
    • Take two arguments: path_to_file and content.
    • Append new content at a file located in the path designated in the arguments.
    • DO: Append some content to the file you made above.
  6. all_dir_count.py:
    • Take one argument: path (to directory).
    • Count all directories and their respective subdirectories.
    • Print the total count of directories found in designated path into stdout.
    • DO: Scan the directories dir provided.
  7. file_line_count.py:
    • Take one argument: (path to) filename.
    • Count all lines in file.
    • Print total count of lines into stdout.
    • Refactor your script to take in multiple arguments
      • Example: $ python file_line_count.py file1.txt file2.txt file3.txt
      • Count all each line in each file.
      • Print the total count of lines from all files into stdout.
    • DO: Scan all the txt files located in the docs directory provided.
  8. scan_log_with_keyword.py
    • Take three arguments: path_to_file, output_filename and keyword.
    • Scan each line of a log file and only pull out lines that contain the keyword given.
    • Print the count of lines that matched into stdout.
    • The script should also create a new file with results from the scan with the name FILENAME_report.txt where FILENAME is dynamic based on the output_filename argument provided.
    • DO Logs to scan (in the logs dir provided):
      • In the nginx.log grab all the lines with a 404 http status code.
      • In the auth.log grabl all the lines with a Connection closed event.
      • In the ufw.log grab all lines where the source connection came from 212.118.253.113
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment