OS function module
os.name view the current system
os.getcwd () to view the current directory
os.listdir (path) to view the file path
os.path.isfile ( '' file path '') to see if the file is
os.path.isdir (above) to see whether it is a folder
os.path.isabs (path) to see whether it is an absolute path
Set absolute path: os.path.abspath (filename)
Rename os.rename ( 'modified path plus file', 'new path to the new name')
Create an empty file os.mknod (filename)
Similarly create a directory os.mkdir (directory name)
Create multi-level directory os.makedirs (path)
Delete the directory os.removedirs (r 'delete path')
Delete files os.remove ( 'filename')
os.getpid () Gets process ID
Modifying the current directory: Modify the os.chdir (r '/ home /') home directory
The catalog file separated out os.path.split (r '/ home / test.txt')
Result ( '/home','test.txt')
You can also obtain a file or folder, respectively:
Get folder: os.path.dirname (path)
Get File: os.path.basename (path)
The combined catalog file: os.path.join (r 'paths to merge', 'file to be merged')
Modify the user input error path: dir = r'c: / work \ 123.txt '
After os.path.normpath (dir) modified to: c: \\ work \\ 123.txt
Content acquisition system commands, save it to a variable
test=os.popen('ifconfig').read()
Function parameter passing:
To specify the type of incoming data is a tuple
, then add the parameters before *
def fun(*a)
return a
fun(1,5,9)
Back tuple (1,5,9)
Similarly, if the incoming data dictionary
, add the parameter before **
def fun2(**a)
return a
fun2(python=1,hello=2)
Returns: { 'python': 1, 'hello': 2}
But if its argument is a list
, for example,
def f(x,li=[]):
for i in range(x):
li.append(i*i)
print (li)
f(4)
f(5)
Print is
[0, 1, 4, 9]
[0, 1, 4, 9, 0, 1, 4, 9, 16]
The reason for this is to print a list of the parameters time, that is, a list of global variables, because he has been allocated memory space in function pretreatment time, the contents to be printed as shown above
Define global variables inside a function, the definition can not be assigned
def fun(a,b):
Global x
x = 3
return a*b*x
print(fun(20,30))
z=x+5
print (z)
Import module
Import All functions: from module import * so that the code can be directly prepared using a function in the module, the module did not have to function.
Built-in modules:
-
sys module
sys.platform inspection system
sys.argv [0] path itself
sys.argv [1] The first parameter acquiring program [2] The second parameter
Parameters may be sys.argv [1:] can obtain a plurality of parameters
-
datatime module
Get the current time: now = datatime.datetime.now ()
Output time mode: now.strftime ( "% d-% m-% Y% I:% M:% S") Y represents a 4-digit year large, small two digits y, I represents H represents a 24-hour 12 hours system
-
optparse
This module will be dedicated on the back of the module written explanation for this purpose, examples of learning
-
hashlib
a = hashlib.md5 () to impart a function pointer md5
a.update('python'.encode('utf-8'))
print (a.hexdigest ()) is printed python md5 value of
Write a md5 crack tools (grammar exercises)