Question

In: Computer Science

Deliverables There is one deliverable for this assignment hw6.py Make sure the script obeys all the...

Deliverables

There is one deliverable for this assignment

  • hw6.py

Make sure the script obeys all the rules in the Script Requirements page.

Specification

The script must have 3 functions:

  • get_args
  • create_python_file
  • print_directory

get_args

This function must have the following header:

def get_args(arg_number):

This function takes as its parameter an integer.

The function should look at the number of command line arguments that the script gets when it is run.

If the number of command line arguments is less than the parameter, it should print a usage message in the form specified in the Class Notes and the function should cause the script to quit.

If it gets the right number of command line arguments it should return those arguments.

Remember that the length of sys.argv is always one more than the number of command line arguments.

The test code will use the first command line argument as the argument to create_python_file and the second command line argument as the argument to print_directory.

create_python_file

This function must have the following header:

def create_python_file(filename):

This function takes as it's parameter a name of a Python script without the .py extension.

The first thing the function should do is check that there is no . (dot) in the filename.

If there is, the function should print an error message and the script should quit.

This error message must contain the word "ERROR".

Otherwise the function should add ".py" to the filename.

It should then use the Unix command touch to create a file with that filename.

touch when run with an argument creates a file of that name.

Then the function should give this file 755 permissions using the Unix chmod command.

print_directory

This function must have the following header:

def print_directory(path):

This function takes as it's parameter a path.

The first thing that the function should do is check to make sure the path is a directory that exits.

If the directory does not exist the function should print an error message and quit.

This error message must contain the word "ERROR".

Otherwise, the function should print the entries in the directory specified by the path parameter.

Script for this assignment

Open an a text editor and create the file hw6.py.

You can use the editor built into IDLE or a program like Sublime.

Test Code

Your hw6.py file must contain the following test code at the bottom of the file:

filename, path = get_args(2)
print(filename, path)
create_python_file(filename)
print_directory(path)

Suggestions

Write this program in a step-by-step fashion using the technique of incremental development.

In other words, write a bit of code, test it, make whatever changes you need to get it working, and go on to the next step.

  1. Create the file hw5.py.
    Write a statement to import the os and sys modules.
    Enter the headers for get_args, create_python_file and print_directory.
    Under each header write the Python statement pass.
    Run the script.
    Fix any errors you find.
  2. Remove the pass statement from get_args.
    Replace it with an if statement that prints an error message if the length of sys.argv is less the value of the parameter arg_number plus 1.
    After the print statement, but still inside the if statement, write at statement that causes the script to quit. Copy the first line of the test code into your script at the bottom of the file.
    Run the script with no arguments.
    You should see the error message,
    Run the script with one argument.
    You should see the error message.
    Run the script with two arguments.
    You should not see your error message, but you will get a TypeError.
    Fix any errors you find.
  3. Outside the if statement write a return statement that that returns the first two elements in sys.argv.
    Copy the second line of the test code into your script at the bottom of the file.
    Run the script with two arguments.
    You should see those two arguments.
    Fix any errors you find.
  4. Replace the text in the print statement with the text of a usage message as described in Class Notes 10.
    Your message should have the same format as shown in the Class Notes and it should print only the name of the script, with no path. Run the script with no arguments.
    You should see
    Usage: hw6.py FILENAME PATH
    Fix any error you find.
  5. Remove the pass statement from create_python_file.
    In it place write an if statement which prints an error message if the argument

Solutions

Expert Solution

(*Note: Please up-vote. If any doubt, please let me know in the comments)

CODE:

import os
import sys

def get_args(arg_number):
if(len(sys.argv) < arg_number + 1):
print("Usage: hw6.py FILENAME PATH")
exit() #quit the script
return sys.argv[1],sys.argv[2]

def create_python_file(filename):
if "." in filename:
print("ERROR : The filename should not have any dot . in it")
exit() #quit the script
filename = filename + ".py"
os.system(f"touch {filename}") #creating the file with touch command
os.system(f"chmod 755 {filename}") #changing UNIX permissions

def print_directory(path):
if not os.path.exists(path):
print("ERROR : The provided path does not exist")
exit()
os.system(f"ls {path}")
  

#test code
filename, path = get_args(2)
print(filename, path)
create_python_file(filename)
print_directory(path)

CODE SCREENSHOT (for indentation ref.):

Test OUTPUTS:

Running without arguments:

Running with proper arguments:


Related Solutions

Deliverables There is one deliverable for this assignment hw4.py Make sure the script obeys all the...
Deliverables There is one deliverable for this assignment hw4.py Make sure the script obeys all the rules in the Script Requirements page. Specification The file has entries like the following Barnstable,Barnstable,1 Bourne,Barnstable,5 Brewster,Barnstable,9 ... This script should create a dictionary where the county is the key and then total number of cases for the country is the value. The script should print the name of the county with the highest number of cases along with the total cases. The script...
Deliverables There is one deliverable for this assignment hw1.py Make sure the script obeys all the...
Deliverables There is one deliverable for this assignment hw1.py Make sure the script obeys all the rules in the Script Requirements page. What is Special About This Assignment This homework assignment is going to be different from the other assignments. You will have to do very little coding for this assignment. Instead, I will supply you with a function that will test regular expressions using regular expressions contained in the following variables: regex_1 regex_2 regex_3 regex_4 regex_5 regex_6 regex_7 regex_8...
Due Sunday, November 1st at 11:59 PM Deliverables There is one deliverable for this assignment hw7.py...
Due Sunday, November 1st at 11:59 PM Deliverables There is one deliverable for this assignment hw7.py Make sure the script obeys all the rules in the Script Requirements page. Specification Your script must print a Kelvin to Fahrenheit conversion table and between a minimum and maximum values, and a Fahrenheit to Kelvin conversion also between a minimum and maximum values. Here is the formula for converting Kelvin to Fahrenheit Here is the formula for converting Fahrenheit to Kelvin The script...
Write a script called script2-4.py that takes a person's delivery order as inputs, totals all the...
Write a script called script2-4.py that takes a person's delivery order as inputs, totals all the items and calculates the tax due and the total due. The number of inputs is not known. You can assume that the input is always valid. (i.e no negative numbers or string like "cat"). Use a loop that takes the prices of items as parameters that are floats, counts the number of items, and sums them to find the total. You must also use...
Write a script called script2-4.py that takes a person's delivery order as inputs, totals all the...
Write a script called script2-4.py that takes a person's delivery order as inputs, totals all the items and calculates the tax due and the total due. The number of inputs is not known. You can assume that the input is always valid. (i.e no negative numbers or string like "cat"). Use a loop that takes the prices of items as parameters that are floats, counts the number of items, and sums them to find the total. You must also use...
Write a script called script2-4.py that takes a person's delivery order as inputs, totals all the...
Write a script called script2-4.py that takes a person's delivery order as inputs, totals all the items and calculates the tax due and the total due. The number of inputs is not known. You can assume that the input is always valid. (i.e no negative numbers or string like "cat"). Use a loop that takes the prices of items as parameters that are floats, counts the number of items, and sums them to find the total. You must also use...
Make sure to include comments that explain all your steps (starts with #) Make sure to...
Make sure to include comments that explain all your steps (starts with #) Make sure to include comments that explain all your steps (starts with #) Write a program that prompts the user for a string (a sentence, a word list, single words etc.), counts the number of times each word appears and outputs the total word count and unique word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt...
Assignment Requirements Write a python application that consists of two .py files. One file is a...
Assignment Requirements Write a python application that consists of two .py files. One file is a module that contains functions used by the main program. NOTE: Please name your module file: asgn4_module.py The first function that is defined in the module is named is_field_blank and it receives a string and checks to see whether or not it is blank. If so, it returns True, if not it return false. The second function that is defined in the module is named...
ASSIGNMENT Discuss the concept of "proof" as it relates to science. Make sure to provide at...
ASSIGNMENT Discuss the concept of "proof" as it relates to science. Make sure to provide at least one example in your discussion to facilitate the concept of proof.
the assignment is to compare and contrast two ads. Make sure the ads are similar enough...
the assignment is to compare and contrast two ads. Make sure the ads are similar enough to be compared and different enough to be contrasted: two soap ads, two cereal ads, two ads that are for baby products, etc 1 page
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT