http://
python复制、删除文件代码、python代码出错重新启动
python遍历和删除指定文件夹下全部的pyc文件
网页抓取、阅读PDF/Word文档、与Excel电子表格交互、解析CSV/JSON文件、调度任务、发送邮件和SMS文本、基于Pillow模块的图像处理、通过GUI自己主动化控制键盘和鼠标
python实现文件复制
利用windows copy命令实现将一个文件夹中的文件拷贝到还有一个文件夹
from os import listdir, pathimport subprocessif __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 fnmatchfrom os import walk, path, removeimport sysif 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文件。
python代码出错重新启动
执行python代码某个片断出错。能够重新启动本代码继续执行
try: ...except: time.sleep(2) subprocess.call(['python', sys.argv[0]])
其他方法[]
计算身份证最后一位校验码
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)
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 ioloopconfig_parser.py - My implementation of python standard library's ConfigParser moduledancing_links.py - My implementation of Dr. Knuth's dancing links algorithm, with a demo to solve N-Queen problemdisjoint_set.py - Disjoint set is a very important data structure, this is my naive implementationfileinput.py - My implementation of python standard library's fileinput modulego_repl.py - A REPL for golang, support executing Go statements with instant feedbackhtml_template.py - A simple html template engine, supporting similar syntax as Django template languagelisp.py - A Lisp parser implemented in python, inspired by Peter Novig's essaymemento.py - Very elegant memento design pattern impl, copied from activestate recipespatch_module.py - Patch python modules lazily, only when they are importedquine.py - A python script to print itselfrpc.py - Simplistic RPC for pythontimeit.py - My partial implementation of standard library's timeit moduleemojify - Render an image with emoji's based on the colors in original imageweb_terminal - A remote console from a web browseronline_judge - A OJ system like leetcode, with a small problemset, supporting only python solutionsimage_crawler - A web image crawler written based on Tornadohttp_server - A basic http server supporting static files/wsgi apps/proxying[]
使用python批量下载文件
[]
[]
[]
from:
ref: