Pythonman (eight)

Source: Internet
Author: User

Hello Python:)

Module:
A module is a file that contains all of the functions and variables that you define, followed by a. PY name. Modules can be introduced by other programs, and functions such as those in the module have been used. Introduction Keyword: import. Modules must be imported before they can use a method function inside the module, just like a package in Java.

OS modules: Operating system modules (Adaptive various OS)
Import OS
Os GETCWD () #返回当前工作目录
Os.chdir (PATH) #改变当前工作目录
Os.listdir (path =?) #列举指定目录的文件名
Os.mkdir (PATH) #创建目录
Os.makedirs (PATH) #递归创建目录
Os.remove (PATH) #删除文件
Os.rmdir (path) #传入目标目录, delete empty directory
Os.removedirs (PATH) #递归删除目录
Os.rename (old, new) #重命名
Os.system (command) # Shell command to run the system
Os.curdir #指代当前目录
Os.pardir #指代当前的父目录
Os.seq #输出操作系统热定的路径分隔符
Os.linesep #当前平台的行终止符
Os.name #指代当前的操作系统

Pickle Module (Kimchi module ... )
This module can store almost Python objects in binary, a process known as: storing pickling, reading unpickling

Import Pickle
My_list = [' Kepler ', ' Air ']
Pickle_file = open (' Mylist.pkl ', ' WB ')
Pickle.dump (My_list, Pickle_file)
Pickle_file.close ()
#以上是把my_list这个列表存放为二进制文件, pay attention to the use of RB when reading, and read it with the load function.


Exception
Try
Something Code ...
Except Exception[as reason]:
Something Code ...
Finally
Something Code ...

Raise: Actively triggering an exception.

Else
For most languages, else can only be paired with if, but Python is different!
If version:
...
While version:
While the else under while normally exits the loop when the while is, the note is normal exit, no break interrupt exit, will execute else
For version:
Same while version

No problem, let's do it:
Try
Int ("123")
Except
Print ("Something wrong!!!")
Else
Print ("Everything is OK!!!")

#输出Everything is OK!!!

With
Introduction: When opening a file, the last to close the file, may be forgotten at any time, and now with the debut, some seem less complicated
! With version:
Try
f = open ("Data.txt". ' W ')
For line in F:
Print (line)
Except
Print ("Wrong happened")
Finally
F.close ()

With version:
Try
With open ("Data.txt", ' W ') as F:
For line in F:
Print (line)
Except
Print ("Wrong happened")

Pythonman (eight)

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.