​十个常见的 Python 脚本 (详细介绍 + 代码举例)

以下是十个常见的Python脚本,每个脚本都有详细的描述和代码示例:

  1. 计算器脚本:



# 计算器脚本
 
def calculator(expression):
    return eval(expression)
 
# 使用示例
result = calculator("1 + 2 * 3")
print(result)  # 输出: 7
  1. 简单的交互式提示符脚本:



# 简单的交互式提示符脚本
 
import cmd
 
class SimplePrompt(cmd.Cmd):
    def do_greet(self, name):
        "Greet user"
        print(f"Hello, {name}!")
 
# 使用示例
SimplePrompt().cmdloop()
  1. 文件分割器脚本:



# 文件分割器脚本
 
def split_file(file_path, chunk_size=1024):
    with open(file_path, 'rb') as file:
        chunk = file.read(chunk_size)
        while chunk:
            yield chunk
            chunk = file.read(chunk_size)
 
# 使用示例
for chunk in split_file('example.txt', 100):
    print(chunk)
  1. 简单的网页抓取脚本:



# 简单的网页抓取脚本
 
import requests
 
def fetch_website(url):
    response = requests.get(url)
    if response.status_code == 200:
        return response.text
    else:
        return "Error fetching website"
 
# 使用示例
content = fetch_website('https://www.example.com')
print(content)
  1. 简单的排序算法(冒泡排序):



# 冒泡排序算法
 
def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n - i- 1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1]= arr[j+1], arr[j]
    return arr
 
# 使用示例
arr = [64, 34, 25, 12, 22, 11, 90]
sorted_arr = bubble_sort(arr)
print("Sorted array is:", sorted_arr)
  1. 简单的文件下载器脚本:



# 简单的文件下载器脚本
 
import requests
 
def download_file(url, file_path):
    response = requests.get(url)
    with open(file_path, 'wb') as file:
        file.write(response.content)
 
# 使用示例
download_file('https://www.example.com/image.jpg', 'image.jpg')
  1. 简单的文件查看器脚本:



# 简单的文件查看器脚本
 
import os
 
def view_file(file_path):
    with open(file_path, 'r') as file:
        print(file.read())
 
# 使用示例
view_file('example.txt')
  1. 简单的文件加密器脚本:



# 简单的文件加密器脚本
 
import os
 
def encrypt_file(file_path, key):
    with open(file_path, 'rb') as file:
        data = file.read()
        encrypted_data = bytes([(ord(x) ^ key) for x in data])
        return encrypted_data
 
# 使用示例
encrypted_data = encrypt_file('example.txt', 123)
with open('example_encrypt
最后修改于:2024年09月02日 09:32

评论已关闭

推荐阅读

Vue中使用mind-map实现在线思维导图
2024年08月04日
VUE
Web前端最全Vue实现免密登录跳转的方式_vue怎么样不登录返回首页,最强技术实现
2024年08月04日
VUE
vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)
2024年08月04日
VUE
Vue-颜色选择器实现方案——>Vue-Color( 实战*1+ Demo*7)
2024年08月04日
VUE
Vue项目卡顿慢加载?这些优化技巧告诉你!_vue数据多渲染卡顿
2024年08月04日
VUE
vue中的keep-alive详解与应用场景
2024年08月04日
VUE
Vue、React实现excel导出功能(三种实现方式保姆级讲解)
2024年08月04日
vue-office/docx插件实现docx文件预览
2024年08月04日
VUE
java调用js文件的两种方法(支持V8引擎)
2024年08月04日
JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js
2024年08月04日
两周从爬虫小白变大神 _yjs_js_security_passport
2024年08月04日
JS笔记(对象、函数、数组)
2024年08月04日
Markdown.js:强大的纯JavaScript Markdown解析器
2024年08月04日
Vue项目:js模拟点击a标签下载文件并重命名,URL文件地址下载方法、请求接口下载文件方法总结。
2024年08月04日
vue 父组件怎么获取子组件里面的data数据
2024年08月04日
VUE
个人开发实现AI套壳网站快速搭建(Vue+elementUI+SpringBoot)
2024年08月04日
el-table 表格封装并改造实现单元格可编辑
2024年08月04日
none
nodejs环境下创建vue项目、SSH密钥登陆!!!
2024年08月04日
vue+quill+element-ui实现视频、图片上传及缩放保姆级教程,轻松使用富文本
2024年08月04日
【three.js】22. Imported Models导入模型
2024年08月04日