In: Computer Science
Deliverables
There is one deliverable for this assignment
Make sure the script obeys all the rules in the Script Requirements page.
Specification
The script must have 3 functions:
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.
Usage: hw6.py FILENAME PATHFix any error you find.
(*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: