Python rookie Diary 7

Source: Internet
Author: User
Tags switches

1. If command-line operations are performed directly in the operating system, the interface functions provided by the operating system are generally used, and if you want to invoke the operating system's interface functions in Python to manipulate files, directories, The OS and Os.path modules can be used to directly invoke the interface functions provided by the operating system

2. >>>os.name, the name attribute will reflect some information about the operating system, if it is POSIX, the system is Linux, Unix or Mac OS X, if it is NT, is the Windows system, to obtain detailed system information, You can call the uname () function, noting that the uname () function is not available on Windows, that is, some functions of the OS module are operating system-related

3. Environment variables defined in the operating system, all stored in the Os.environ variable, to get the value of an environment variable, you can call Os.environ.get (' key '), where key is the environment variable name, change what you want to know

4. For functions that operate the directory, part is placed in the OS module, part of the Os.path module
such as:>>> Os.path.abspath ('. ') #查看当前的绝对路径
' D:\\python '
>>> os.path.join (' D:\\python ', ' TestDir ') #在前面目录下添加一个新目录的路径, never use a connector here, because connector expressions on different operating systems are not the same
' D:\\python\\testdir '
>>> os.mkdir (' D:\\python\\testdir ') #然后就是用mkdir函数在已有的路径下创建目录 to complete the process of adding directories under the operating system
>>> os.rmdir (' D:\\python\\testdir ') #删除该路径下的目录
>>> os.path.split (' D:\\python\\testdir ') #通过os. Path.split () function, which splits a path into two parts, followed by a directory or file name that is always the last level:
(' D:\\python ', ' TestDir ')

5. Functions for manipulating files are also part of the OS module, part of which is placed in the Os.path module
For example: >>> os.path.splitext (' D:\\python\\testdir\\abc.txt ') #os. Path.splitext () allows you to get file extensions directly Name, very convenient for many times
(' D:\\python\\testdir\\abc ', '. txt ')
>>>os.rename (' test.txt ', ' test.py ') #将当前目录下的文件名改为后者, note that the file under the current directory is modified
The functions for merging and splitting paths within 4,5 do not require directories and files to be real, they operate only on strings.

6. The Shutil module provides CopyFile () functions, and you can find a number of utility functions in the Shutil module, which can be seen as additions to the OS module.

7. >>> [x for X in Os.listdir ('. ') if Os.path.isdir (x)] #列出当前目录下的所有目录
[' DLLs ', ' Doc ', ' include ', ' Lib ', ' Libs ', ' Scripts ', ' tcl ', ' Tools ']

8. >>> [x for X in Os.listdir ('. ') if Os.path.isfile (x) and Os.path.splitext (x) [1]== '. Py '] #列出当前目录下的所有. py file
[' function as return value. py ', ' class experiment. Py ']

9. There are two ways to define a dictionary: ①.d = dict (name= ' Bob ', age=20, score=88)
②.D = {' name ': ' Bob ', ' age ': $, ' score ': 88}

10. In the process of running the program, all variables are in memory, you can modify the variable at any time, but once the program is finished, the memory occupied by the variable is all recycled by the operating system. If the modified value is not stored on the disk, the next time you rerun the program, the variable is initialized to the original value, such as when we define a dictionary, the key corresponding to the values of the changes, this is only in memory to modify, if not stored as a hard disk, then the end of the program, memory is recycled, The modified values are also deleted, so the process of changing variables from memory to storage or transfer is called serialization, and Python provides the Pickle module for serialization. In turn, re-reading the variable contents from the serialized object into memory is called deserialization, i.e. unpickling

11. Serialized Instance:>>> Import Pickle
>>>d={' A ': 1, ' B ': 2, ' C ': 3}
>>> f = open (' D:/all study Source/python Test/ceshi.txt ', ' WB ')
>>> Pickle.dump (d, F)
>>> F.close ()
Deserialization instance:>>> f = open (' D:/all study Source/python Test/ceshi.txt ', ' RB ')
>>> d = pickle.load (f)
>>> F.close ()
>>> D
{' B ': 2, ' C ': 3, ' a ': 1}

Pickle's problem is the same as any other programming language-specific serialization problem, which is that it can only be used in Python, and that different versions of Python may not be compatible with each other, so you can only use pickle to save unimportant data
It doesn't matter if we're going to be able to serialize objects between different programming languages if we want to pass the object to a standard format, like XML, but the better way is to serialize it to JSON, because JSON means that a string can be read by all languages. It can also be conveniently stored on disk or transmitted over a network. JSON is not only a standard format, but also faster than XML.

Python's built-in JSON module provides a very complete translation of Python objects into JSON format

14. The operating system turns each task to perform alternately, Task 1 executes 0.01 seconds, switches to Task 2, Task 2 executes 0.01 seconds, then switches to Task 3, executes 0.01 seconds ... This is done repeatedly. On the surface, each task is executed alternately, but because the CPU is executing too fast, we feel as if all the tasks are executing at the same time, for the operating system, a task is a process, and some processes not only do one thing at a time, such as word, It can do typing, spell checking, printing, and so on at the same time. Within a process, to do multiple tasks at the same time, you need to run multiple "subtasks" at the same time, and we refer to these "subtasks" in the process as threads (thread).

15. To summarize, there are 3 ways to achieve multi-tasking: Multi-process mode, multi-threading mode, multi-process + multi-threading mode.

16. Performing multiple tasks at the same time is usually not unrelated to each task, but requires communication and coordination with each other, sometimes task 1 must pause waiting for task 2 to finish before continuing, sometimes task 3 and task 4 cannot be executed simultaneously, so The complexity of multi-process and multi-threaded programs is much higher than the one-process single-threaded program we wrote earlier, because the complexity is high and debugging is difficult, so it is not a necessity, we do not want to write multi-tasking. However, there are many times when there is no more task to do. Think of the movie on the computer, you must be a thread to play the video, another thread play audio, otherwise, the single-threaded implementation can only play the video before playing the audio, or the audio playback before playing video, which is obviously not possible, Python supports both multi-process and multi-threaded.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python rookie Diary 7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.