python小工具

时间:2023-03-09 17:38:12
python小工具

http://blog.****.net/pipisorry/article/details/46754515

python复制、删除文件代码、python代码出错重新启动

python遍历和删除指定文件夹下全部的pyc文件

网页抓取、阅读PDF/Word文档、与Excel电子表格交互、解析CSV/JSON文件、调度任务、发送邮件和SMS文本、基于Pillow模块的图像处理、通过GUI自己主动化控制键盘和鼠标

python实现文件复制

利用windows copy命令实现将一个文件夹中的文件拷贝到还有一个文件夹

from os import listdir, path
import subprocess if __name__ == "__main__":
INPUT_DIR = r'E:\Entertain\Videos'
OUTPUT_DIR = r'C:\Users\pi\Desktop\out'
all_output_dir_filenames = listdir(OUTPUT_DIR)
all_output_dir_filenames.append('desktop.ini') for file_name in listdir(INPUT_DIR):
if file_name not in all_output_dir_filenames:
filename = path.join(INPUT_DIR, file_name)
# print(filename)
subprocess.Popen(["copy", filename, OUTPUT_DIR], shell=True)

python遍历和删除指定文件夹下全部的pyc文件

E:\mine\python_workspace\Utility\DelPyc.py:





#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'pi'
__mtime__ = '7/29/2015-029'
"""
import fnmatch
from os import walk, path, remove
import sys if len(sys.argv) >= 3:
EXT = sys.argv[2]
DEL_DIR = sys.argv[1]
elif len(sys.argv) >= 2:
EXT = 'pyc'
DEL_DIR = sys.argv[1]
else:
EXT = 'pyc'
DEL_DIR = r'E:\mine\python_workspace\WebSite'
if not path.exists(DEL_DIR):
print('error: DEL_DIR not found!!!')
exit()
print('DEL_DIR: ', DEL_DIR, '\ndelete file extension: ', EXT) print('deleted files:\n') def del_pyc(DEL_DIR):
for filepath, _, filename_list in walk(DEL_DIR):
for filename in filename_list:
if fnmatch.fnmatch(filename, '*.' + EXT): # unix shell风格匹配方式
# if filename.endswith('.pyc'):
print(filename)
remove(path.join(filepath, filename)) if __name__ == '__main__':
del_pyc(DEL_DIR)

Note:上面的程序是针对当前pycharm中打开的py文件相应的文件夹删除当中全部的pyc文件。假设是直接执行(而不是在以下的tools中执行)。则删除E:\mine\python_workspace\WebSite文件夹下的pyc文件。

皮皮blog

python代码出错重新启动

执行python代码某个片断出错。能够重新启动本代码继续执行

try:
...
except:
time.sleep(2)
subprocess.call(['python', sys.argv[0]])

其他方法[python实现自己主动重新启动本程序的方法]

计算身份证最后一位校验码

def identifier():
'''
计算身份证最后一位校验码
'''
id_card_str = '42028118921027721'
x = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2]
coefficient = np.array([7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]) id_card = np.array(list(id_card_str), dtype=int)
x_id = id_card.dot(coefficient) % 11
id_card_str += str(x[x_id])
print(id_card_str)

皮皮blog

Python日常任务自己主动化

日常繁琐任务(Python)自己主动化指南

内容包含网页抓取、阅读PDF/Word文档、与Excel电子表格交互、解析CSV/JSON文件、调度任务、发送邮件和SMS文本、基于Pillow模块的图像处理、通过GUI自己主动化控制键盘和鼠标等

[书:Automate the Boring Stuff with Python]这本书正在翻译中

[(Udemy):日常繁琐任务(Python)自己主动化指南免费课程]

开源:解决有意思问题的Python脚本集合

A collection of python scripts that solve interesting problems.
async_sched.py - A asynchronous scheduler implemented using coroutines, in principle similar to Tornado's ioloop
config_parser.py - My implementation of python standard library's ConfigParser module
dancing_links.py - My implementation of Dr. Knuth's dancing links algorithm, with a demo to solve N-Queen problem
disjoint_set.py - Disjoint set is a very important data structure, this is my naive implementation
fileinput.py - My implementation of python standard library's fileinput module
go_repl.py - A REPL for golang, support executing Go statements with instant feedback
html_template.py - A simple html template engine, supporting similar syntax as Django template language
lisp.py - A Lisp parser implemented in python, inspired by Peter Novig's essay
memento.py - Very elegant memento design pattern impl, copied from activestate recipes
patch_module.py - Patch python modules lazily, only when they are imported
quine.py - A python script to print itself
rpc.py - Simplistic RPC for python
timeit.py - My partial implementation of standard library's timeit module
emojify - Render an image with emoji's based on the colors in original image
web_terminal - A remote console from a web browser
online_judge - A OJ system like leetcode, with a small problemset, supporting only python solutions
image_crawler - A web image crawler written based on Tornado
http_server - A basic http server supporting static files/wsgi apps/proxying

[Beautifully constructed python scripts]

使用python批量下载文件

[Python下载文件的方法]

[用Python的requests模块下载文件]

[Python实现批量下载文件]

from:http://blog.****.net/pipisorry/article/details/46754515

ref:python系统模块sys、os和路径、系统命令

python文件夹遍历和删除指定文件夹下的pyc文件

如何遍历移除项目中的全部 .pyc 文件