python复制、删除文件代码、python代码出错重新启动
python遍历和删除指定文件夹下全部的pyc文件
网页抓取、阅读PDF/Word文档、与Excel电子表格交互、解析CSV/JSON文件、调度任务、发送邮件和SMS文本、基于Pillow模块的图像处理、通过GUI自己主动化控制键盘和鼠标
python实现文件复制
利用windows copy命令实现将一个文件夹中的文件拷贝到还有一个文件夹
fromos importlistdir, path
importsubprocess
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')
forfile_name inlistdir(INPUT_DIR):iffile_name not inall_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'"""importfnmatch
fromos importwalk, path, remove
importsys
iflen(sys.argv) >=3:EXT =sys.argv[2]
DEL_DIR =sys.argv[1]
eliflen(sys.argv) >=2:EXT ='pyc'DEL_DIR =sys.argv[1]
else:EXT ='pyc'DEL_DIR =r'E:\mine\python_workspace\WebSite'if notpath.exists(DEL_DIR):print('error: DEL_DIR not found!!!')
exit()
print('DEL_DIR: ', DEL_DIR, '\ndelete file extension: ', EXT)
print('deleted files:\n')
defdel_pyc(DEL_DIR):forfilepath, _, filename_list inwalk(DEL_DIR):forfilename infilename_list:iffnmatch.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文件。
python代码出错重新启动
执行python代码某个片断出错。能够重新启动本代码继续执行
try:...except:time.sleep(2)
subprocess.call(['python', sys.argv[0]])
计算身份证最后一位校验码
defidentifier():'''计算身份证最后一位校验码'''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) %11id_card_str +=str(x[x_id])
print(id_card_str)
Python日常任务自己主动化
日常繁琐任务(Python)自己主动化指南
内容包含网页抓取、阅读PDF/Word文档、与Excel电子表格交互、解析CSV/JSON文件、调度任务、发送邮件和SMS文本、基于Pillow模块的图像处理、通过GUI自己主动化控制键盘和鼠标等
开源:解决有意思问题的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
使用python批量下载文件