Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use Python: Path Adjuster Finish the codes below: A Unix file path consists of a

ID: 3834878 • Letter: U

Question

Use Python:

Path Adjuster

Finish the codes below:

A Unix file path consists of a series of directory names, starting with a slash and separated by forward slash characters. For example, /usr/local/bin/ has three directories (the final directory may or may not be followed by a slash) Complete the changePath() function in the "paths.py" file, which takes two string arguments, representing a current path and a new "updated" path (a string of directory names) with which to update the current path, according to the following rules 1. If the updated path begins with a forward slash, it is an absolute path, and should completely replace the current path 2. Avalid directory name (letters or digits, followed by a slash) should just be appended to the current path, with a single forward slash separating the two. For example, "Nar/ib" plus "postfix/" would produce "/var/lib/postfix/ 3. A directory "name" of exactly (two consecutive periods and a slash) means "go up one level to the preceding directory", so the last/latest directory name should be removed from the current path. The sole exception to this is if the current path is only a single slash (the root directory, in which case you should ignore the and proceed to the next directory in the updated path. Be careful of removing the final (topmost) directory! "/usr/local/bin/" plus "../share/man/" would produce "/usr/local/share/man/ (the eliminates "bin/). Likewise, "Nar/tmp/ plus would simply produce "M" (the first two instances of remove "tmp/" and "var, but you can't go back before the starting slash) You may find the string method rfind helpful here; it returns the index of the final (rightmost) occurrence of a particular substring. 4. For simplicity, your code should make sure that the current path always begins with a single forward slash; if it doesn't have one, be sure to add it! 5. Forward slashes will always appear one at a time (i.e., you should never see "Ir in a path) change Path returns a string representing the new file path

Explanation / Answer

Code:

#!/usr/bin/python

import os

# script name: change_path.py

# script to change path for a given path

def changePath(initial, changes):

   result = ""

   # checking on Rule 4

   if (not initial.startswith('/')):

       initial = '/' + initial

   # checking on Rule 5

   if '//' in initial or '//' in changes:

       return None

   # Rule 1 satisfied

   if(changes.startswith('/')):

       result = changes

   # Rule 2 satisfied

   elif(changes.endswith('/') or changes.find('/') == -1):

       result = initial + '/' + changes

   # Checking Rule 3

   elif(changes.rfind('..') != -1):

       rel_count = changes.count('..')

       length = len(changes)

       index = changes.rfind('..') + 2

       #print "changes: ", changes[index:length]

       initial_str = []

       initial_str = initial.split('/')

       initial_str = initial_str[1:len(initial_str)-rel_count]

       initial_str ='/' + '/'.join(initial_str)

       #print "initial: ", initial_str  

       changes_len = len(changes[index:length])

       #print "changes len: ", changes_len

       if(len(initial_str) == 1 and initial_str == '/'):

           result = changes[index:length]

       else:

           result = initial_str + changes[index:length]

      

   return result

if __name__=='__main__':

   start = raw_input("Enter the starting path: ")

   update = raw_input("Enter the path modification: ")

  

   #print("Final path: %s" % changePath(start, update))

   result = changePath(start, update)

   if(result == ""):

       print "Final path: /"

   else:

       print("Final path: %s" % result)

Execution and output:

186590cb0725:Python bonkv$ python change_path.py

Enter the starting path: /usr/share/tmp

Enter the path modification: foo

Final path: /usr/share/tmp/foo

186590cb0725:Python bonkv$ python change_path.py

Enter the starting path: /var/man/db

Enter the path modification: /etc/tmp

Final path: /etc/tmp

186590cb0725:Python bonkv$ python change_path.py

Enter the starting path: /this/is/a/long/path

Enter the path modification: ../../different/path

changes len: 15

Final path: /this/is/a/different/path

186590cb0725:Python bonkv$ vi change_path.py

186590cb0725:Python bonkv$ python change_path.py

Enter the starting path: /this/is/a/long/path

Enter the path modification: ../../different/path

Final path: /this/is/a/different/path

186590cb0725:Python bonkv$ python change_path.py

Enter the starting path: /foo/bar

Enter the path modification: ../../baz

Final path: /baz

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote