Dive into python3 重点摘录(chapter3)

    科技2024-06-23  70

    Python 3 comes with a module called os, which stands for “operating system.” The os module contains a plethora of functions to get information on — and in some cases, to manipulate — local directories, files, processes, and environment variables. Python does its best to offer a unified api across all supported operating systems so your programs can run on any computer with as little platform-specific code as possible.The glob module is another tool in the Python standard library. It’s an easy way to get the contents of a directory programmatically, and it uses the sort of wildcards that you may already be familiar with from working on the command line.A list comprehension provides a compact way of mapping a list into another list by applying a function to each of the elements of the list.List comprehensions can also filter items, producing a result that can be smaller than the original list.Here’s a trick with dictionary comprehensions that might be useful someday: swapping the keys and values of a dictionary.Of course, this only works if the values of the dictionary are immutable, like strings or tuples. If you try this with a dictionary that contains lists, it will fail most spectacularly.
    Processed: 0.010, SQL: 8