File Manipulation with Python
Scenarios are quite common where an IT professional is tasked with modifying a file on many computer systems or many files on a single computer or a combination of the two. For example, a large software may need the copyright notice comment to be changed on every source file in a project. Or, a configuration file may need to be modified on every server in a multi-server deployment. Doing this task manually is not a viable way to approach the problem. Instead, a better solution is to use a scriptable environment to perform search and replace functionality on the files in question. Python is an excellent tool for this kind of task.
Samba is a unix/linux file sharing suite of programs designed to provide Windows interoperable file and print sharing services. Much of the configuration for the Samba daemon is provided by the text file smb.conf (renamed to smb.txt for this assignment). After an update was deployed to three dozen linux servers it was discovered that a couple of configured parameters were incorrect. Unfortunately, since there are unique aspects to each server, copying the contents of a single file to each server is not a viable solution. To fix the problem, the IT department must connect to each server, edit the smb.conf file, and restart the smbd daemon. To automate this task, each server will have a Python script copied to it. Then an SSH session will be initiated to each server from which the script will be executed. Your task is to write the Python script.
The invocation of the script must be as follows:
python modify.py fileSpec “from” “to”
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”
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. Note that sys.argv 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 do a 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!
Just to get you started, the following takes the arguments from the command line and opens the input file and an output file:
fileSpec = sys.argv# read file name first argument
searchText = sys.argv# text to search for
replaceText = sys.argv# 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
This CSIS-354 - IT Assignment has been solved by our IT experts at TV Assignment Help. Our Assignment Writing Experts are efficient to provide a fresh solution to this question. We are serving more than 10000+ Students in Australia, UK & US by helping them to score HD in their academics. Our Experts are well trained to follow all marking rubrics & referencing style.
Be it a used or new solution, the quality of the work submitted by our assignment experts remains unhampered. You may continue to expect the same or even better quality with the used and new assignment solution files respectively. There’s one thing to be noticed that you could choose one between the two and acquire an HD either way. You could choose a new assignment solution file to get yourself an exclusive, plagiarism (with free Turnitin file), expert quality assignment or order an old solution file that was considered worthy of the highest distinction.
Proofreading and Editing$9.00Per Page
Consultation with Expert$35.00Per Hour
Live Session 1-on-1$40.00Per 30 min.
Doing your Assignment with our resources is simple, take Expert assistance to ensure HD Grades. Here you Go....