Python Practice One

Source: Internet
Author: User
Tags uuid


#题目链接: Https://


1. Backing up files

[email protected] ~]# cat import timeimport ossource=['/root/py/', '/root/tx ']target= '/tmp/' today_dir= Target+time.strftime ('%y%m%d ') time_dir=time.strftime ('%h%m%s ') touch =today_dir+os.sep+time_dir+ '. zip ' Command_ Touch = "ZIP-QR" +touch+ "+". Join (source) print (Command_touch) if os.path.exists (Today_dir) ==false:os.mkdir (Today_ DIR) if Os.system (Command_touch) ==0:print (' Success backup up ') else:print (' Failed back ') [[email protected] ~]#


#参考链接: PIL Example

From PIL import image,imagefont,imagedrawimport osdef modify_image (image_path,num=1): (Image_Path) mod _img=imagedraw.draw (IMG) myfont=imagefont.truetype (r "C:\Windows\Fonts\Verdana.ttf", Mod_img.text (img.size[1) *3/4,0), str (num), fill= (255,0,0), Font=myfont) save_path=os.path.dirname (image_path) +os.sep+ "Modify" + Os.path.splitext (Image_path) [1] (save_path) if __name__== "__main__": Path=r "C:\Users\chauncey\Desktop\ C.jpg "Modify_image (path,4)

650) this.width=650; "src=" Https:// "title=" Modify.jpg "alt=" Wkiol1mkpyvtjoinaaaok863cok601.jpg "/>


import uuiddef create_code (Num,length):     result=[]    while true:        uid_4= Uuid.uuid4 ()         uid_1=uuid.uuid1 ()          x=str (uid_1). Replace ('-', ')         y=str (uid_4). Replace ('-', ')         result.append ((x+y) [: length])          if len (Result) ==num:             break    for code in result:         print (code) if __name__== ' __main__ ':     create_code (200,21) 
================= restart:c:/users/chauncey/desktop/ 5e742c9c7ccd11e79a0ef5e742c9d7ccd11e7b164f5e742c9e7ccd11e7a0baf5e742c9f7ccd11e79768f5e742ca07ccd11e79c95f5e742ca17ccd11e7 9d15f5e742ca27ccd11e7b8d2f5e742ca37ccd11e79cd6f5e742ca47ccd11e794d7f5e742ca57ccd11e79356f5e742ca67ccd11e7851cf5e742ca77cc D11E790FBF5E7453AE7CCD11E7983EF5E7453AF7CCD11E78775F5E7453B07CCD11E79590F5E7453B17CCD11E7A4F6F ...

0002 References: pymysql example

#mysqlmysql > delete from py_uuid_0002; query ok, 0 rows affected  (0.00 sec) mysql> describe py_uuid_0002;+- ----------+-------------+------+-----+---------+-------+| field     | type         | Null | Key | Default |  extra |+-----------+-------------+------+-----+---------+-------+| uuid_name | varchar (64)  | YES  |     | NULL    |        |+-----------+-------------+------+-----+---------+-------+1 row in  set  (0.00 SEC) mysql> select count (*)  from py_uuid_0002;+----------+|  count (*)  |+----------+|      201 |+----------+1 row in  set  (0.00 sec) #pythonimport  uuidimport pymysqldef&Nbsp;create_code (num,length):    result=[]    while true:         uid_4=uuid.uuid4 ()          UID_1=UUID.UUID1 ()         x=str (uid_1). Replace ('-', ')          y=str (uid_4). Replace ('-', ') '          result.append ((x+y) [: Length])         if len (result) ==num:             break    return  resultdef mysql_conn (host= "", port=3307,user= "root", passwd= "123456", dbname= "test"):     db=pymysql.connect (Host,user,passwd,dbname,port=port)     cursor= Db.cursor ()     return db,cursordef mysql_exec (Db,cursor,result):     for uuid_code  in result:        sql= "insert into py_uuid_0002  VALUES (' "+uuid_code+"); "         try:             cursor.execute (SQL)              Db.commit ()         except:             db.rollback ()         if __name__== ' __main__ ':     result=create_code (201,21)     db,cursor=mysql_conn ()     mysql_exec (Db,cursor,result)     db.close ()

0003: Reference Link: Redis build redis List

Import uuidimport redisdef create_code (num,length):    result=[]     while true:        uid_4=uuid.uuid4 ()          UID_1=UUID.UUID1 ()         x= STR (uid_1). Replace ('-', ')         y=str (uid_4). Replace ('-', ')          result.append ((x+y) [: length])          if len (Result) ==num:             Break    return resultdef save_redis (Result):     r=redis. Redis (host= ' ', port=7000,db=0)     for uuid_code in result:         r.lpush (' Uuid_code1 ', uuid_code) if __name__== ' __main__ ':     result=crEate_code (200,21)     save_redis (Result) 
[[email protected] bin]#/redis-cli-p 7000127.0.0.1:7000> lrange ' uuid_code1 ' 5) "C2df0cfc7d0211e7b85ef" 2) "c2df0 CFB7D0211E78E3FF "3)" c2df0cfa7d0211e79ce3f ">

This article is from the "Chauncey" blog, make sure to keep this source

Python Practice One

Related Article

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: 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.