用python写小脚本确实是一件好玩的事情,因为不是个大活儿,而且能解决眼边前十分繁琐的事情,这种轻松且便宜的代码颇受人民群众的欢迎~有点生活小妙招的意味
大家较为熟知的脚本是用python来做爬虫、抢票、签到、自动回复机器人、批量处理文件等,这些比较常规,还有些较复杂的,比如做物品识别、语义分析、图像处理等,只要你有需求场景,总会想到办法写个小脚本去处理它。
这个项目不是什么牛逼的大程序,而是作者在日常工作和python学习过程中积累的脚本,一个脚本解决一个问题。获得1万9的赞,说明颇有群众基础。
初学者可以先按照这种模式来学习编程,从解决问题的角度来写代码,把python当作一把锤子,不断找钉子。
很多人会觉得看似简单,但就是写不来,几十行的代码如何能拼拼凑凑变成脚本。我觉得基础不好的更是需要多看语法、多敲代码,不然只能停留在望洋兴叹的阶段。学习这件事从来都是讲方法、看行动的,如果你不知道编程的架构,那买一本python书,仔细研究目录,python有哪些部分组成就能一清二楚。或者跟着老师学,完成每一道编程作业题,也能快速掌握。
分享其中几个脚本:
1、检查主目录中是否存在某文件夹,若不存在则创建
Description : Checks to seeif 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)+a1]
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、翻转数字
n=int(input("Enter number: "))
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
print("Reverse of the number:",rev)
5、PDF转音频
import pyttsx3 import pyPDF2
book =open('book.pdf','rb')
pdfreader=pyPDF2.PdfFileReader(book) pages=pdfreader.numPages print(pages)
speaker=pyttsx3.init() page=pdfreadergetpage(7) text=page.extractText() speaker.say(text) speaker.runAndWait()
最后:
如果想快速实现零基础入门,建议往下看,Python学习必须经历这几个阶段:(附相关书籍+视频课程)
一、Python所有方向的学习路线
Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、Python必备开发工具
四、Python视频合集
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
五、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
六、Python练习题
检查学习结果。
七、面试资料
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
上述这份完整版的Python全套学习资料已经上传CSDN官方,如果需要可以微信扫描下方CSDN官方认证二维码 即可领取
👉[[CSDN大礼包:《python安装包&全套学习资料》免费分享]](安全链接,放心点击)
![](https://i-blog.csdnimg.cn/blog_migrate/7b8c90c2d16b553e99981fadc4911d23.png)