os module today
play

os Module Today File I/O from last time Behind the scenes, this - PowerPoint PPT Presentation

os Module Today File I/O from last time Behind the scenes, this module loads a module for your particular operating system Slides 38-48 The os and sys modules Regardless of your actual OS, Python imports os The exec()


  1. os Module Today • File I/O from last time • Behind the scenes, this module loads a module for your particular operating system – Slides 38-48 • The os and sys modules • Regardless of your actual OS, Python imports os • The exec(…) BIF – See Section 15.1 in the Python Library Reference • Confirming parameter types • Confirming parameter types – Revisit raising exceptions • Lots of goodies, particularly file system utilities – e.g. , os.sep is the directory separator for your OS • Passing by reference • Lists of lists and dictionaries • The next few slides have a selection of file-related • Finding minimums and maximums functions • Timing code execution Winter 2011 CISC101 - Whittaker 1 Winter 2011 CISC101 - Whittaker 2 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod os Module - Cont. os Module - Cont. Deletes a file Gets the current working directory • remove(…) • getcwd() Renames a file Creates a directory • rename(…) • mkdir(…) Generates filenames in a directory Removes a directory • walk(…) • rmdir(…) tree (generator object) Verify permission modes • access(…) Changes the working directory Changes the working directory • chdir(…) • chdir(…) Changes permission modes Changes permission modes • chmod(…) • chmod(…) Changes root directory of current • chroot(…) process in Unix • listdir(…) Lists files and folders in a directory Winter 2011 CISC101 - Whittaker 3 Winter 2011 CISC101 - Whittaker 4 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  2. os.path Module os.path Module – Cont. • basename(…) Returns the name of a file • getmtime(…) Returns file modification time Returns the name of a directory • dirname(…) Returns file size in bytes • getsize(…) Joins a directory and a filename • join(…) Does file or directory name exist? • exists(…) Splits a path into a directory and • split(…) Is this a directory name and does • isdir(…) a filename (a tuple) it exist? it exist? • splitext(…) Returns filename and extension Is this a file and does it exist? • isfile(…) as tuple • getatime(…) Returns last file access time • getctime(…) Returns file creation time Winter 2011 CISC101 - Whittaker 5 Winter 2011 CISC101 - Whittaker 6 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod os Module - Demo os Module - Cont. • The os module also has many commands that • LargeFileSearch.py allow you to run other non-Python code and programs from within your program • Uses a recursive directory search – e.g. , os.system() allows you to run a system – Don’t worry about this technique command (such as a DOS command) – You are not responsible for knowing how to use – You are not responsible for knowing how to use recursion … yet Winter 2011 CISC101 - Whittaker 7 Winter 2011 CISC101 - Whittaker 8 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  3. Aside – the exec() BIF sys Module • See Section 27.1 in the Python Library Reference • Also in os module • Contains more system functions and attributes • Can execute Python code if it is supplied to the – A small sampling is provided here BIF as a string – The string could come from a file, for example • argv – A list of all command line parameters sent to the – A list of all command line parameters sent to the • Demo: RemoteFileExecution.py interpreter • builtin_module_names – A list of all built-in modules • exit(0) – Immediately exits a Python program giving a zero exit status code Winter 2011 CISC101 - Whittaker 9 Winter 2011 CISC101 - Whittaker 10 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod The isinstance(…) BIF sys Module - Cont. • When you get an argument value mapped to a • getwindowsversion() parameter, how do you know it is the right type? – Returns a tuple consisting of major , minor , build , platform , and service pack status • The function assumes that the function is invoked with the proper types • path • But should you check, and how can you? – A list of the module search paths used by Python • Suppose you have a parameter called param and • Suppose you have a parameter called param and • platform • platform it is supposed to be a string – The current OS platform • prefix isinstance(param, str) – The folder where Python is located • version will return True , False otherwise – The version of Python being used Winter 2011 CISC101 - Whittaker 11 Winter 2011 CISC101 - Whittaker 12 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  4. isinstance(…) - Cont. Raising Exceptions - Revisited • What other types can you check? • So, what do you in your function if your parameter type is not correct? – bool – int float complex • Demo: RaiseExceptionIsInstance.py – str – list tuple set dict range – list tuple set dict range • Many other types exist in Python – You can have an object type as well Winter 2011 CISC101 - Whittaker 13 Winter 2011 CISC101 - Whittaker 14 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Passing by Reference Passing by Reference - Observations • Can a function change something in its parameter • Immutable objects do not stay changed outside list and have the change stay (or “stick”) when the the function function is done? – That’s the int , the string and the tuple – All you can do inside the function is assign a new value to the parameter • What kinds of parameters can be changed and • Re-assigning a mutable object does not change it • Re-assigning a mutable object does not change it how? • However, some actions allow the changes to stay after the function is complete • Demo: TestPassingByReference.py – Element-by-element changes using the slice operator – Invoking a method belonging to a list Winter 2011 CISC101 - Whittaker 15 Winter 2011 CISC101 - Whittaker 16 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  5. Passing by Reference - Cont. Passing by Reference - Cont. • When you pass a list (or any object) into a • We can take advantage of being able to pass function, you do not re-create the entire structure mutable objects (especially lists) by reference to inside the function simplify code! – That would be wasteful and time-consuming! • This also gives you a way to get more than one • Instead you just pass a reference (a memory thing out of a function without having to return a tuple of lists tuple of lists address, or “pointer”) into the function address, or “pointer”) into the function – But , if you are doing this maybe your function is not just • If the object is mutable, and its elements are doing one thing! changed or deleted inside the function, then that – And , returning multiple things through the parameter change is made to the structure created outside list can make for confusing code the function Winter 2011 CISC101 - Whittaker 17 Winter 2011 CISC101 - Whittaker 18 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Lists of Lists Lists of Lists - Example • We know a list can hold anything >>> for value in ex2: – The elements do not even have to be the same type print(value) ex1 = [1, 4.0, ‘abc’, 2, ‘hello!’] 4.5 [1, 2, 'abc'] [1, 2, 'abc'] • So, there is no reason that an element cannot be 7 another list (or a tuple, or some other collection) hello ex2 = [4.5, [1, 2, ‘abc’], 7, ‘hello’] Winter 2011 CISC101 - Whittaker 19 Winter 2011 CISC101 - Whittaker 20 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  6. Lists of Lists - Cont. Lists of Lists - Cont. • How can I display the elements in the list at • Nothing new! position 1? • How do I access just the 'abc' string inside the list at position 1? >>> for value in ex2[1]: print(value) print(value) >>> ex2[1][2] = 'wxyz' >>> ex2[1][2] = 'wxyz' >>> ex2 1 [4.5, [1, 2, 'wxyz'], 7, 'hello'] 2 abc Winter 2011 CISC101 - Whittaker 21 Winter 2011 CISC101 - Whittaker 22 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Lists of Lists - Cont. Dictionaries – An Example >>> name1 = {'name':'Sam', 'age':18, 'SN':4445555} • A list of lists can be used represent tabular data >>> name2 = {'name':'Boris', 'age':21, 'SN':5554444} >>> name3 = {'name':'Ben', 'age':19, 'SN':5445444} • Suppose you wish to store people’s names, ages >>> allNames = [name1, name2, name3] and student numbers? >>> allNames [{'age': 18, 'name': 'Sam', 'SN': 4445555}, {'age': 21, 'name': 'Boris', 'SN': 5554444}, {'age': 19, 21, 'name': 'Boris', 'SN': 5554444}, {'age': 19, 'name': 'Ben', 'SN': 5445444}] ex3 = [['Sam', 18, 4445555], ['Boris', 21, 5554444], ['Ben', 19, 5445444]] >>> allNames[2]['age'] 19 • You could do it this way, or (better yet) use a >>> allNames[1]['name'] dictionary 'Boris' Winter 2011 CISC101 - Whittaker 23 Winter 2011 CISC101 - Whittaker 24 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend