用python写小脚本是一件好玩的事情,因为不是个大活儿,而且能解决眼边前十分繁琐的事情,这种轻松且便宜的代码颇受人民群众的欢迎~有点生活小妙招的意味

大家较为熟知的脚本是用python来做爬虫、抢票、签到、自动回复机器人、批量处理文件等,这些比较常规,还有些较复杂的,比如做物品识别、语义分析、图像处理等,只要你有需求场景,总会想到办法写个脚本去处理它。

github上有个python项目,里面提供了几百个(可能上千)小脚本,涉及到算法、文件、文本、图像、视频、音乐、爬虫、邮件、可视化、系统、下载等各种常用场景的处理脚本。

项目地址:https://github.com/geekcomputers/Python

这个项目不是什么牛逼的大程序,而是作者在日常工作和python学习过程中积累的脚本,一个脚本解决一个问题。获得1万9的赞,说明颇有群众基础。

作者在介绍中所说,他并非专业程序员,而是为了解决问题、提高效率写了这些代码。我也是鼓励初学者可以先按照这种模式来学习编程,从解决问题的角度来写代码,把python当作一把锤子,不断找钉子。

分享其中几个脚本:

1、检查主目录中是否存在某文件夹,若不存在则创建文件

# Description   : Checks to see if a directory exists in the users home directory, if not then create it

import os  # Import the OS module

MESSAGE = 'The directory already exists.'
TESTDIR = 'testdir'
try:
    home = os.path.expanduser("~")  # Set the variable home by expanding the user's set home directory
    print(home)  # Print the location

    if not os.path.exists(os.path.join(home, TESTDIR)):  # os.path.join() for making a full path safely
        os.makedirs(os.path.join(home, TESTDIR))  # If not create the directory, inside their home directory
    else:
        print(MESSAGE)
except Exception as e:
    print(e)

2、打印图片分辨率

def jpeg_res(filename):
   """"This function prints the resolution of the jpeg image file passed into it"""

   # open image for reading in binary mode
   with open(filename,'rb'as img_file:

       # height of image (in 2 bytes) is at 164th position
       img_file.seek(163)

       # read the 2 bytes
       a = img_file.read(2)

       # calculate height
       height = (a[0] << 8) + a[1]

       # next 2 bytes is width
       a = img_file.read(2)

       # calculate width
       width = (a[0] << 8) + a[1]

   print("The resolution of the image is",width,"x",height)

jpeg_res("img1.jpg")

3、连接MySQL数据库

import mysql.connector

# MySQl databses details

mydb = mysql.connector.connect(
    host="0.0.0.0",
    user="root",
    passwd="",
    database="db_name"
)
mycursor = mydb.cursor()

# Execute SQL Query =>>>> mycursor.execute("SQL Query")
mycursor.execute("SELECT column FROM table")

myresult = mycursor.fetchall()

for x in myresult:
    print(x)

4、PDF转音频

import pyttsx3
import pyPDF2
book = open('book.pdf','rb')
pdfreader = pyPDF2.PdfFileReader(book)
pages = pdfreader.numPages
print(pages)
speaker = pyttsx3.init()
page= pdfreader.getpage(7)
text = page.extractText()
speaker.say(text)
speaker.runAndWait()


©著作权归作者所有:来自51CTO博客作者mb5fe18fab305a5的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 小白如何入门Python爬虫
  2. Oracle19c_cdb数据库自动安装部署脚本
  3. Python异步爬虫进阶必备,效率杠杠的!
  4. 钻漏洞,他用爬虫非法获取 1500 万余条个人信息,倒卖获利 54 万被判
  5. 小伙Python爬虫并自制新闻网站,太好玩了
  6. 1 个自动化脚本搞死公司,是碰瓷么?看后续调查结果
  7. 1 个 Python 自动化脚本引发的惨案!把公司搞死了
  8. 用两种非传统的爬虫,我把CSDN博主的底裤都给爬出来了!
  9. 爬虫进阶:反反爬虫技巧

随机推荐

  1. 重装linux,从ubuntu到centos
  2. 双插槽与单插槽内存模型?
  3. Linux socket通信——并发服务器(fork)
  4. ubuntu下的Samba配置:使每个用户可以用自
  5. linux c (4) 进程终止-exit和_exit函数
  6. linux上/proc/cpuinfo中的bug是什么意思?
  7. 导出内存(linux中)
  8. 詹金斯死了,但是pid文件存在
  9. Linux BT下载(3)-与Tracker交互
  10. 【linux】下的mkfifo 命令 和【C语言】中