Question
The invocation of the script must be as follows: python modify.py fileSpec from to where, modify.py is the name of your script fileSpec is the
The invocation of the script must be as follows:
python modify.py fileSpec "from" "to"
where,
modify.py is the name of your script
fileSpec is the name of the file to be modified (smb.txt)
"from" is the text to be searched for (be sure to enclose any white space in quotes.
"to" is the text the searched text is to be replaced with.
Testing the script is your responsibility. However a good test case is:
python modify.py smb.txt "password sync = yes" "password sync = no"
Hints
Accessing command line arguments in Python requires the sys module. Consequently, your script must have the "import sys" directive. To access individual arguments, use sys.argv[x] where x is the argument number. I.e. the first argument would be accessed by sys.argv[1]. Note that sys.argv[0] is the name of the script and not used in this assignment.
Specifying command line arguments from an IDE differs depending on the IDE. For PyCharm CE, select Run -> Edit Configurations and place them on the Parameters field. For example:
Also, recognize that you will open two files, one for reading and one for writing. I highly recommend that you append a ".new" to the file to be written while debugging. Otherwise, you will corrupt your input file while testing.
How do you open a file in Python for reading or writing? It is very easy, to open for reading:
inPointer = open(fileSpec, 'r')
where "fileSpec" is the filename and 'r' tells Python that you want to read the file. The variable inFile is sometimes called a "pointer" to the file. Essentially it is a reference that allows one to operate on the file.
Similarly, to open a file for writing:
outPointer = open(fileSpec,'w')
where fileSpec is the same as the reading example and 'w' tells Python the file is for writing. Just as inPointer, outPointer is a "pointer" to the opened file to write.
Now what? To read from the file you can read one line at a time, all the lines, or the entire file into a string. See python.org for all available methods but the following will read the next line of a file into a string.
line = inFile.readline()# reads the next line of text into a string
Once you read the contents, you need to search for occurrences of "from" and replace them with "to". How? Strings have a replace method such that you can loop through the lines and search and replace for each occurrence. Next write the modified line to a new file.
Once you get to the end of the file, close both the read file and the write file and you're done!
The following takes the arguments from the command line and opens the input file and an output file:
import sys
fileSpec = sys.argv[1]# read file name first argument
searchText = sys.argv[2]# text to search for
replaceText = sys.argv[3]# text to replace with
outFile = fileSpec + '.new'# write file = read file with .new appended
outPointer = open(outFile,'w')# open write file
inPointer = open(fileSpec, 'r')# open read file
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started