Python Common knowledge points

Source: Internet
Author: User

A.isdigit ()
Judging is not a pure number, is True
Import string
If I in String.digits: Determine if the number is stored

Os.listdir (' home ')
Lists all files, only one layer, in list form
Os.listdir ('. ') List files

Os.path.dirname (__file__)
Current directory, which is the directory where the script is located

Os.path.abspath (' tmp ')
TMP the absolute path to this file or folder

Os.path.isdir (' home ')
Judge is not a directory, is true
Os.path.isfile (' home ')
Judge is not a file, is False

Os.path.join ('/home ', ' xxx ')
Results:/home/xxx
Does not determine if it is correct, only the path is connected

Os.path.exists (Self.workdir):
To determine the existence of a directory

Os.mkdir (XXX)
Create a Directory

Os.remove ()
deleting files

Os.chdir (XXX)
Go to Directory

Modify Directory Permissions
Os.system ("Chown mysql:mysql")

Get directory Size
Os.path.getsize ()

Os.kill (int (PID number), 15) 15 is the signal
End Process

Dic.has_key (MD5)
Determine if MD5 is DIC's key

To do a traversal of the dictionary,
For k,v in Dic.items ():
Get rid of the dictionary minutes notice
For k,v in Dic.items ():
Dmi[k]=v.strip[]


Add key and value to dictionary
In [5]: dic[' A ']=[' B ']
In [6]: DIC
Out[6]: {' A ': [' B ']}


With open (xx, ' W ') opens as FD with W:
Fd.write (XX)

From subprocess import Popen, PIPE
Referencing shell commands
such as: P=popen (' ifconfig ', shell=true)

From subprocess Import Popen,pipe
p = Popen ([' Pidof ', ' MD '],stdout=pipe)
P.stdout.read (). Strip Remove/n

Open ('/etc/passwd '). Read () Show all the contents

__FILE__ The representative called itself, and the same as the

Convert a dictionary to a list
I-I in Dict
I.strip () remove \ n
I.strip (). replace (' # ', ') Change # to Empty

Strip removing spaces
A[0].strip if 0 is a space or newline character, the resulting is false
If there is content, the technology is true

List into dictionaries
>>>list1 = [' Key1 ', ' key2 ', ' Key3 ']j:
>>>list2 = [' 1 ', ' 2 ', ' 3 ']
>>>dict (Zip (list1,list2))

10-second delay output
Import time
Time.sleep (10)

Count a few lines
Data.count (' \ n ')

Standard error output, is not output error
Print >> sys.stderr, "xxx"
PY 12.py 2>/dev/null

I.strip () Remove line break, tab

Remove empty lines
I for I in Data.split (' \ n ') if I


From SUBPROCSS import Popen,pipe
P=popen (' vim ', stdout=pipe,stderr=pipe)
P.communicate () Do not view results


Add Time
Start=time.time ()
Time.sleep (3)
End=time.time ()
Pring '%0.2f '% (end-start) View how many seconds to run

The append () method adds a new element to the tail of the list







Python Common knowledge points

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.