溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

學(xué)習(xí)Python能實現(xiàn)的實際功能有哪些

發(fā)布時間:2020-08-04 17:18:39 來源:億速云 閱讀:185 作者:Leah 欄目:編程語言

學(xué)習(xí)Python能實現(xiàn)的實際功能有哪些?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

1、批量修改文件后綴

本例子使用Pythonos模塊和argparse模塊,將工作目錄work_dir下所有后綴名為old_ext的文件修改為后綴名為new_ext。通過本例子,大家將會大概清楚argparse模塊的主要用法。

導(dǎo)入模塊

import argparse

import os

定義腳本參數(shù)

def get_parser():

parser = argparse.ArgumentParser(

description=' 工 作 目 錄 中 文 件 后 綴 名 修 改 ') parser.add_argument('work_dir', metavar='WORK_DIR', type=str, nargs=1,

help='修改后綴名的文件目錄')

parser.add_argument('old_ext', metavar='OLD_EXT',

type=str,nargs=1,help='原來的后綴)

parser.add_argument('new_ext', metavar='NEW_EXT',

type=str, nargs=1, help='新的后綴')

return parser

后綴名批量修改

 

def batch_rename(work_dir, old_ext, new_ext):

"""

傳遞當(dāng)前目錄,原來后綴名,新的后綴名后,批量重命名后綴

"""

for filename in os.listdir(work_dir):

# 獲取得到文件后綴

split_file = os.path.splitext(filename)

file_ext = split_file[1]

# 定位后綴名為old_ext 的文件

if old_ext == file_ext:

# 修改后文件的完整名稱

newfile = split_file[0] + new_ext

# 實現(xiàn)重命名操作

os.rename(

os.path.join(work_dir, filename), os.path.join(work_dir, newfile)

)

print(" 完 成 重 命 名 ")

print(os.listdir(work_dir))

 

實現(xiàn)Main

 

def main():

"""

main函數(shù)

"""

# 命令行參數(shù)

parser = get_parser()

args = vars(parser.parse_args())

# 從命令行參數(shù)中依次解析出參數(shù)

work_dir = args['work_dir'][0]

old_ext = args['old_ext'][0]

if old_ext[0] != '.':

old_ext = '.' + old_ext

new_ext = args['new_ext'][0]

if new_ext[0] != '.':

new_ext = '.' + new_ext

        

batch_rename(work_dir, old_ext, new_ext)

 

2、計算日期

#計算指定日期當(dāng)月最后一天的日期和該月天數(shù)import datetime

import calendar

init_date = datetime.date.today()

print(' 當(dāng) 前 給 定 時 間 :', init_date) current_month_days=calendar.monthrange(init_date.year,init_date.month)[1] print(calendar.month(2019,init_date.month))

current_month_last_day = datetime.date(init_date.year, init_date.month, current_month_days)

print("當(dāng)月最后一天:",current_month_last_day)

print("該月天數(shù):",current_month_days)

 

當(dāng)前給定時間: 2019-12-08

December 2019

Mo   Tu     We   Th     Fr      Sa     Su

                                                        1

2       3       4       5       6       7       8

9       10     11     12     13     14     15

16     17     18     19     20     21     22

23     24     25     26     27     28     29

30     31    

當(dāng)月最后一天: 2019-12-31

該月天數(shù): 31

 

3、批量壓縮文件

import zipfile   # 導(dǎo)入zipfile,這個是用來做壓縮和解壓的Python模塊;

import os import time

 

def batch_zip(start_dir):

start_dir = start_dir         # 要壓縮的文件夾路徑

file_news = start_dir + '.zip'    # 壓縮后文件夾的名字

 

z = zipfile.ZipFile(file_news, 'w', zipfile.ZIP_DEFLATED)

for dir_path, dir_names, file_names in os.walk(start_dir):

# 這一句很重要,不replace的話,就從根目錄開始復(fù)制

f_path = dir_path.replace(start_dir, '')

f_path = f_path and f_path + os.sep        # 實現(xiàn)當(dāng)前文件夾以及包含的所有文件的壓縮

for filename in file_names:

z.write(os.path.join(dir_path, filename), f_path + filename)

z.close()

return file_news

 

batch_zip('./data/ziptest')

 

4、turtle繪制奧運五環(huán)圖

turtle繪圖的函數(shù)非常好用,基本看到函數(shù)名字,就能知道它的含義,下面使用turtle,僅用15行代碼來繪制奧運五環(huán)圖。

1)導(dǎo)入庫

import turtle

2)定義畫圓函數(shù)

def drawCircle(x,y,c='red'):

p.pu()# 抬起畫筆

p.goto(x,y) # 繪制圓的起始位置p.pd()#     放 下 畫 筆p.color(c)# 繪制c色圓環(huán)p.circle(30,360) #繪制圓:半徑,角度

3)畫筆基本設(shè)置

p = turtle

p.pensize(3) # 畫筆尺寸設(shè)置3

4)繪制五環(huán)圖

調(diào)畫圓函數(shù)

drawCircle(0,0,'blue')

drawCircle(60,0,'black')

drawCircle(120,0,'red')

drawCircle(90,-30,'green')

drawCircle(30,-30,'yellow')

p.done()

結(jié)果:

學(xué)習(xí)Python能實現(xiàn)的實際功能有哪些

5、32位加密

import hashlib

# 對字符串s實現(xiàn)32位加密

 

 def hash_cry32(s):

m = hashlib.md5()

m.update((str(s).encode('utf-8')))

return m.hexdigest()

 

print(hash_cry32(1))        # c4ca4238a0b923820dcc509a6f75849b

print(hash_cry32('hello'))       # 5d41402abc4b2a76b9719d911017c592

 

關(guān)于學(xué)習(xí)Python能實現(xiàn)的實際功能有哪些問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI