Python modules (standard, custom, third-party modules), Python OS, sys and time modules, and __name__.
One, the Python module
The essence of the import module is to import the code inside the module, from top to bottom to execute again. The order in which the modules are found is, first, the current directory, and then the environment variable. Modules include: standard modules, custom modules, and third-party modules.
Standard module: Python comes with, do not need to install, direct import can be used;
Custom modules: Python written by yourself
Third-party module: Others develop a good function module, use need to install.
Installation method: (1) system command, pip install xxx install a module,
PIP List view installed modules
Pip Uninstall XXX Unload module
PIP module, inside the Python3 is self-contained, the installation of Python can be used directly, Python2 does not have the module, you need to install before use. Easy_install usage with PIP.
(2) manual installation, first download the installation package, unzip and execute the Python setup.py install in this directory
Second, Python standard module--os
Common functions
import os
1 Print(OS.GETCWD ())#fetch Current Working directory2Os.chmod ("/usr/local", 7)#add permissions to files/directories3 Print(Os.chdir (R"C:\Users\Zy\Desktop\python"))#change the current directory4 Print(OS.GETCWD ())5 Print(Os.makedirs (R"Test1\test2"))#Create A parent directory recursively when a folder is created and the parent directory does not exist6 Print(Os.removedirs (R"Test1\test2"))#recursively delete empty directories7 Print(Os.remove (R"E:\byz_code\day4\a.txt"))#Deleting Files8 Print(Os.listdir ('.'))#list all files in one directory9Os.rename ("Test","test1")# RenamingTen Print(Os.environ)#environment variables for the current operating system One Print(__file__)#represents the current file A Print(Os.path.abspath ('bb.py'))#Get absolute path - Print(Os.path.dirname (Os.path.dirname (__file__)))#Get Parent Directory - Print(Os.path.exists ("hhaaa"))#whether the directory/file exists the Print(Os.path.isfile ("bb.py"))#determine if it is a file - Print(Os.path.isdir ("/usr/local"))#whether it is a path - Print(Os.path.join ("Root",'hehe','haha','A.log'))#stitching into a path
Other OS-related functions
1 Print(Os.curdir)#current directory2 Print(Os.pardir)#Parent Directory3 Print(Os.mkdir (R'test1'))#Create a folder4 Print(Os.rmdir ("test1"))#delete the specified folder and delete only the empty folder5 Print(OS.SEP)#path delimiter for current operating system6 Print(r'%s'%OS.LINESEP)#line breaks for the current operating system7 Print(OS.PATHSEP)#The delimiter for each path in the current system's environment variables, Linux is:, Windows is;8 Print(Os.path.dirname ("/usr/local"))#Get Parent Directory9 Print(Os.path.split ("/usr/hehe/hehe.txt"))#split path and file nameTen Print(Os.path.basename ("/usr/local"))#gets the last level, if the file displays the filename, if the directory displays the directory name One Print(Os.path.getatime ("len_os.py"))#output last access time A Print(Os.path.getmtime ("len_os.py"))#output last access time
Simple instance, delete the Gz file in the current directory
Import= Os.listdir ('. ' ) for in file_list: if'gz' inch file: os.remove (file)
Third, Python standard module--sys
Import SYS Print # sys.argv command line argument list, the first element is the path of the program itself Print # get version information for Python interpreter Print (Sys.path) # Sys.path #返回模块的搜索路径, using the value of the PYTHONPATH environment variable when initializing Print # return operating system platform name sys.exit ('xxx'# exit the program and print xxx, exit normally exits (0)
Where SYS.ARGV usage, print (SYS.ARGV) in the clean_log.py file, you can output the content,
C:\users\user\desktop\python\day6\day6>python clean_log.py a b C
[' clean_log.py ', ' A ', ' B ', ' C ']
Iv. Python standard module--time
Common functions
Import Timetime.sleep (1) Print # take the current time stamp
Other functions
Import datetime, time #一种是时间戳, one is format time, one is a tuple
1 Print(Time.timezone)#time difference from standard time, unit is S2 Print(Int (time.time ()))#gets the current timestamp #时间戳的意思就是从unix元年 (1970.1.1), up to the present number of seconds3 Print(Time.sleep (1))#Rest a few s4 Print(Time.gmtime ())#convert timestamp to time tuple, default time stamp for standard Time zone if not passed5 Print(Time.localtime ())#Converts the timestamp to a time tuple and, if not passed, the timestamp of the current time zone by default6 Print(Time.mktime (Time.localtime ()))#convert a time tuple to a timestamp7 Print(Time.strftime ("%y.%m.%d%h:%m:%s"))#convert a time tuple to a formatted output string8 Print(Time.strptime ("20160204191919","%y%m%d%h%m%s"))#convert formatted time to a time tuple9 Print(Time.struct_time)#Time TupleTen Print(Time.asctime ())#time tuple converted to formatted time One Print(Time.ctime (149776000))#Timestamp converted to formatted time A Print(Datetime.datetime.now ())#current time formatted output - Print(Datetime.datetime.now () + Datetime.timedelta (3))#3 days from the time - Print(Datetime.datetime.now () + Datetime.timedelta (-3))#3 days before the time
Wu, __name__
defSayhi (name):Print('hi~%s'%name)if __name__=='__main__':#The following code will be executed only when you run your own Python file, and will not be executed when you import it in another module . Print('This is the time in your own file:',__name__) Sayhi (' World')#I'm calling when I'm self-rated
Python Basics five--modules