例如,访问某个网站一直不通,需要确定此地址是否可访问,服务器返回什么,进而确定问题在于什么。完成这个任务,如果一味希望采用编译型语言来编写这样的代码,实践中的时间和精力是不够的,这个时候就需要发挥脚本的神奇作用!
毫不夸张的说,能否写出高效实用的脚本代码,直接影响着一个程序员的幸福生活[下班时间]。下面整理 8 个实用的 Python 脚本,需要的时候改改直接用,建议收藏!
1.解决 linux 下 unzip 乱码的问题。
import os
import sys
import zipfile
import argparse
s = ‘\x1b[%d;%dm%s\x1b[0m’
def unzip(path):
file = zipfile.ZipFile(path,“r”)
if args.secret:
file.setpassword(args.secret)
for name in file.namelist():
try:
utf8name=name.decode(‘gbk’)
pathname = os.path.dirname(utf8name)
except:
utf8name=name
pathname = os.path.dirname(utf8name)
#print s % (1, 92, ’ >> extracting:'), utf8name
#pathname = os.path.dirname(utf8name)
if not os.path.exists(pathname) and pathname != “”:
os.makedirs(pathname)
data = file.read(name)
if not os.path.exists(utf8name):
try:
fo = open(utf8name, “w”)
fo.write(data)
fo.close
except:
pass
file.close()
def main(argv):
######################################################
for argparse
p = argparse.ArgumentParser(description=‘解决unzip乱码’)
p.add_argument(‘xxx’, type=str, nargs=‘*’, \
help=‘命令对象.’)
p.add_argument(‘-s’, ‘–secret’, action=‘store’, \
default=None, help=‘密码’)
global args
args = p.parse_args(argv[1:])
xxx = args.xxx
for path in xxx:
if path.endswith(‘.zip’):
if os.path.exists(path):
print s % (1, 97, ’ ++ unzip:'), path
unzip(path)
else:
print s % (1, 91, ’ !! file doesn’t exist.'), path
else:
print s % (1, 91, ’ !! file isn’t a zip file.'), path
if name == ‘main’:
argv = sys.argv
main(argv)
2.统计当前根目录代码行数。
coding=utf-8
import os
import time
设定根目录
basedir = ‘./’
filelists = []
指定想要统计的文件类型
whitelist = [‘cpp’, ‘h’]
#遍历文件, 递归遍历文件夹中的所有
def getFile(basedir):
global filelists
for parent,dirnames,filenames in os.walk(basedir):
for filename in filenames:
ext = filename.split(‘.’)[-1]
#只统计指定的文件类型,略过一些log和cache文件
if ext in whitelist:
filelists.append(os.path.join(parent,filename))
#统计一个的行数
def countLine(fname):
count = 0
把文件做二进制看待,read.
for file_line in open(fname, ‘rb’).readlines():
if file_line != ‘’ and file_line != ‘\n’: #过滤掉空行
count += 1
print (fname + ‘----’ , count)
return count
if name == ‘main’ :
startTime = time.clock()
getFile(basedir)
totalline = 0
for filelist in filelists:
totalline = totalline + countLine(filelist)
print (‘total lines:’,totalline)
print (‘Done! Cost Time: %0.2f second’ % (time.clock() - startTime))
3.扫描当前目录和所有子目录并显示大小。
‘’’
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
‘’’
import os
import sys
try:
directory = sys.argv[1]
except IndexError:
sys.exit(“Must provide an argument.”)
dir_size = 0
fsizedicr = {‘Bytes’: 1,
‘Kilobytes’: float(1) / 1024,
‘Megabytes’: float(1) / (1024 * 1024),
‘Gigabytes’: float(1) / (1024 * 1024 * 1024)}
for (path, dirs, files) in os.walk(directory):
for file in files:
filename = os.path.join(path, file)
dir_size += os.path.getsize(filename)
fsizeList = [str(round(fsizedicr[key] * dir_size, 2)) + " " + key for key in fsizedicr]
if dir_size == 0: print (“File Empty”)
else:
for units in sorted(fsizeList)[::-1]:
print ("Folder Size: " + units)
4.将源目录240天以上的所有文件移动到目标目录。
import shutil
import sys
import time
import os
import argparse
usage = ‘python move_files_over_x_days.py -src [SRC] -dst [DST] -days [DAYS]’
description = ‘Move files from src to dst if they are older than a certain number of days. Default is 240 days’
args_parser = argparse.ArgumentParser(usage=usage, description=description)
args_parser.add_argument(‘-src’, ‘–src’, type=str, nargs=‘?’, default=‘.’, help=‘(OPTIONAL) Directory where files will be moved from. Defaults to current directory’)
args_parser.add_argument(‘-dst’, ‘–dst’, type=str, nargs=‘?’, required=True, help=‘(REQUIRED) Directory where files will be moved to.’)
args_parser.add_argument(‘-days’, ‘–days’, type=int, nargs=‘?’, default=240, help=‘(OPTIONAL) Days value specifies the minimum age of files to be moved. Default is 240.’)
args = args_parser.parse_args()
if args.days < 0:
args.days = 0
src = args.src # 设置源目录
dst = args.dst # 设置目标目录
days = args.days # 设置天数
now = time.time() # 获得当前时间
if not os.path.exists(dst):
os.mkdir(dst)
for f in os.listdir(src): # 遍历源目录所有文件
if os.stat(f).st_mtime < now - days * 86400: # 判断是否超过240天
if os.path.isfile(f): # 检查是否是文件
shutil.move(f, dst) # 移动文件
5.扫描脚本目录,并给出不同类型脚本的计数。
import os
import shutil
from time import strftime
logsdir=“c:\logs\puttylogs”
zipdir=“c:\logs\puttylogs\zipped_logs”
zip_program=“zip.exe”
for files in os.listdir(logsdir):
if files.endswith(“.log”):
files1=files+“.”+strftime(“%Y-%m-%d”)+“.zip”
os.chdir(logsdir)
os.system(zip_program + " " + files1 +" "+ files)
shutil.move(files1, zipdir)
os.remove(files)
6.下载Leetcode的算法题。
‘’’
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
‘’’
import sys
import re
import os
import argparse
import requests
from lxml import html as lxml_html
try:
import html
except ImportError:
import HTMLParser
html = HTMLParser.HTMLParser()
try:
import cPickle as pk
except ImportError:
import pickle as pk
class LeetcodeProblems(object):
def get_problems_info(self):
leetcode_url = ‘https://leetcode.com/problemset/algorithms’
res = requests.get(leetcode_url)
if not res.ok:
print(‘request error’)
sys.exit()
cm = res.text
cmt = cm.split(‘tbody>’)[-2]
indexs = re.findall(r’(\d+)', cmt)
problem_urls = [‘https://leetcode.com’ + url \
for url in re.findall(
r’<a href=“(/problems/.+?)”', cmt)]
levels = re.findall(r"(.+?)", cmt)
tinfos = zip(indexs, levels, problem_urls)
assert (len(indexs) == len(problem_urls) == len(levels))
infos = []
for info in tinfos:
res = requests.get(info[-1])
if not res.ok:
print(‘request error’)
sys.exit()
tree = lxml_html.fromstring(res.text)
title = tree.xpath(‘//meta[@property=“og:title”]/@content’)[0]
description = tree.xpath(‘//meta[@property=“description”]/@content’)
if not description:
description = tree.xpath(‘//meta[@property=“og:description”]/@content’)[0]
else:
description = description[0]
description = html.unescape(description.strip())
tags = tree.xpath(‘//div[@id=“tags”]/following::a[@class=“btn btn-xs btn-primary”]/text()’)
infos.append(
{
‘title’: title,
‘level’: info[1],
‘index’: int(info[0]),
‘description’: description,
‘tags’: tags
}
)
with open(‘leecode_problems.pk’, ‘wb’) as g:
pk.dump(infos, g)
return infos
def to_text(self, pm_infos):
if self.args.index:
key = ‘index’
elif self.args.title:
key = ‘title’
elif self.args.tag:
key = ‘tags’
elif self.args.level:
key = ‘level’
else:
key = ‘index’
infos = sorted(pm_infos, key=lambda i: i[key])
text_template = ‘## {index} - {title}\n’ \
‘{level} {tags}\n’ \
‘{description}\n’ + ‘\n’ * self.args.line
text = ‘’
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!