Python Question We are writing a text editing program that periodically saves a
ID: 3715003 • Letter: P
Question
Python Question
We are writing a text editing program that periodically saves a backup copy of the open edited file whose full file path is stored in string variable fullpath. The name of the backup file starts with "bak_" and is followed by the name of the edited file. For example, if the edited file path is "/home/user/p1.py", then the path name of the backup file is /home/user/bak-pipy. Which code sequence among the following generates the correct path name for the backup file in variable backup_path in a way that does not depend on the operating system? t-os.path.split(fullpath) backup_path -os.path.join(t[0], "bak_"+ t[1]) t-os.path.split(fullpath) backup_pathos.path.join(t[1], "bak_" t[0]) t os.path.split(fullpath) backup_path t[0]""bak_" t[1] tos.path.splitext(fullpath) backup_path - os.path.join(t[e], "bak_"[1])Explanation / Answer
The first option is the correct one as it is OS independant, it can run on Windows as well as Linux. Other options either are wrong or use '/' to seperate the filenames which can be different in different operating systems. the splittext function is generally used to seperate filenames and extensions. So the first option is the most viable option.
what the split function actually does:
It splits the pathname path into a pair, (head, tail) where tail is the last pathname component and head is everything leading up to that. The tail part will never contain a slash; if path ends in a slash, tail will be empty. If there is no slash in path, head will be empty. If path is empty, both head and tail are empty. Trailing slashes are stripped from head unless it is the root (one or more slashes only). In all cases, join(head, tail) returns a path to the same location as path (but the strings may differ).
so t[0] would be everyting upto the folder the file is in and t[1] would be the file name(so you can just add "bak_" to it).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.