Discussion Forum Unit 8 File exception PDF

Title Discussion Forum Unit 8 File exception
Course Programming Fundamentals
Institution University of the People
Pages 3
File Size 43.5 KB
File Type PDF
Total Downloads 3
Total Views 160

Summary

unit 8 file and exception assignment. Precise and accurate work....


Description

While executing, whenever the Python interpreter meets an error it stops the execution. We might run a big program that is supposed to run indefinitely, like an operating system. In this case, we can not let our program break whenever it meets an error. Especially while handling files, the program might face a lot of errors. So we should keep our code prepared for those errors by using try-except block to catch exceptions and stop breaking execution. We will write a file with the string of the code and try to run our program importing the file and see what kind of file errors we find. >>> s = 'import os\ndef crawl(dirname = os.getcwd() ):\n\tfor name in os.listdir(dirname):\n\t\tpath = os.path.join(dirname, name)\n\t\tprint(path)\n\t\tcrawl(path)' >>> with open('file_exc.py', 'w') as f: ... f.write(s) Now our file, 'file_exc.py' is ready with a function 'crawl', which is supposed to crawl and print all the sub-directories in the given directory. So now we will import the module and try the crawl function >>> file_exc.crawl('D:') D:$RECYCLE.BIN D:$RECYCLE.BIN\S-1-5-21-169420739-3919715321-1189473894-1001 D:$RECYCLE.BIN\S-1-5-21-169420739-3919715321-1189473894-1001\desktop.ini Traceback (most recent call last): File "", line 1, in File "G:\Learning\UOPeople\CS 1101\Practice\file_exc.py", line 6, in crawl crawl(path) File "G:\Learning\UOPeople\CS 1101\Practice\file_exc.py", line 6, in crawl crawl(path) File "G:\Learning\UOPeople\CS 1101\Practice\file_exc.py", line 6, in crawl crawl(path) File "G:\Learning\UOPeople\CS 1101\Practice\file_exc.py", line 3, in crawl for name in os.listdir(dirname): NotADirectoryError: [WinError 267] The directory name is invalid: 'D: $RECYCLE.BIN\\S-1-5-21-169420739-3919715321-1189473894-1001\\desktop.ini' We can see our program crawled into 2 sub-directories before it encountered an error. The interpreter threw a NotADirectoryError. Because we wanted to crawl into a file that is not a directory. So we will catch an exception so that our program does not break next time it encounters a NotADirectoryError >>> s1 = s[:s.find('crawl(path)')] + 'try:\n\t\t\tcrawl(path)\n\t\texcept NotADirectoryError:\n\t\t\tprint("not a folder")' >>> with open('file_exc.py','w') as f: ... f.write(s1)

Now our module is ready to handle NotADirectoryError. we restart the interpreter and import the module again and run the function. >>> import file_exc >>> file_exc.crawl('D:') D:$RECYCLE.BIN D:$RECYCLE.BIN\S-1-5-21-169420739-3919715321-1189473894-1001 D:$RECYCLE.BIN\S-1-5-21-169420739-3919715321-1189473894-1001\desktop.ini not a folder D:System Volume Information Traceback (most recent call last): File "", line 1, in File "G:\Learning\UOPeople\CS 1101\Practice\file_exc.py", line 7, in crawl crawl(path) File "G:\Learning\UOPeople\CS 1101\Practice\file_exc.py", line 3, in crawl for name in os.listdir(dirname): PermissionError: [WinError 5] Access is denied: 'D:System Volume Information' We can see our program successfully handled the not directory error but a new type of error is introduced. PermissionError. So we will catch PermissionError exception next time. we go back to our string and modify our code. >>> s1 = open('file_exc.py','r').read() >>> s2 = s1[:s1.find('NotADi')]+ '(PermissionError, NotADirectoryError):\n\t\t\tprint("not folder or not permitted")' >>> with open('file_exc.py','w') as f: ... f.write(s2) Now we modified our code we will restart our interpreter and import the module again. and run it with a list of directories >>> ds = ['D:', 'FakeDirectory'] >>> for directory in ds: ... file_exc.crawl(directory) ... D:$RECYCLE.BIN D:$RECYCLE.BIN\S-1-5-21-169420739-3919715321-1189473894-1001 D:$RECYCLE.BIN\S-1-5-21-169420739-3919715321-1189473894-1001\desktop.ini not folder or not permitted D:System Volume Information not folder or not permitted Traceback (most recent call last): File "", line 2, in File "G:\Learning\UOPeople\CS 1101\Practice\file_exc.py", line 3, in crawl for name in os.listdir(dirname): FileNotFoundError: [WinError 3] The system cannot find the path specified: 'FakeDirectory' We can see our little program successfully handled the permission error this time but a new error is found, FileNotFoundError, because we fed a directory, FakeDirectory, that does not exist, So all we need to do now

is catching another exception. We go back to our string and modify the code and import the module again. >>> s2 = open('file_exc.py','r').read() >>> s3 = s2[:s2.find('NotADirectoryE')] + 'FileNotFoundError, ' + s2[s2.find('NotADirectoryE'):] >>> s3 = s3[:s3.find('"not folder or n')] + '"Found and Error")' >>> with open('file_exc.py','w') as f: ... f.write(s3) >>> import file_exc >>> file_exc.crawl() #remember if no argument is given current directory is the parameter G:\Learning\UOPeople\CS 1101\Practice\venv G:\Learning\UOPeople\CS 1101\Practice\venv\Include G:\Learning\UOPeople\CS 1101\Practice\venv\Lib G:\Learning\UOPeople\CS 1101\Practice\venv\Lib\site-packages G:\Learning\UOPeople\CS 1101\Practice\venv\Lib\site-packages\distutilsprecedence.pth Found and Error G:\Learning\UOPeople\CS 1101\Practice\venv\Lib\sitepackages\easy_install.py Found and Error G:\Learning\UOPeople\CS 1101\Practice\venv\Lib\site-packages\pip G:\Learning\UOPeople\CS 1101\Practice\venv\Lib\sitepackages\pip\_internal G:\Learning\UOPeople\CS 1101\Practice\venv\Lib\sitepackages\pip\_internal\build_env.py Found and Error ........


Similar Free PDFs