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 = ‘’
for info in infos:
if self.args.rm_blank:
info[‘description’] = re.sub(r’[\n\r]+‘, r’\n’, info[‘description’])
text += text_template.format(**info)
with open(‘leecode problems.txt’, ‘w’) as g:
g.write(text)
def run(self):
if os.path.exists(‘leecode_problems.pk’) and not self.args.redownload:
with open(‘leecode_problems.pk’, ‘rb’) as f:
pm_infos = pk.load(f)
else:
pm_infos = self.get_problems_info()
print(‘find %s problems.’ % len(pm_infos))
self.to_text(pm_infos)
def handle_args(argv):
p = argparse.ArgumentParser(description=‘extract all leecode problems to location’)
p.add_argument(‘–index’, action=‘store_true’, help=‘sort by index’)
p.add_argument(‘–level’, action=‘store_true’, help=‘sort by level’)
p.add_argument(‘–tag’, action=‘store_true’, help=‘sort by tag’)
p.add_argument(‘–title’, action=‘store_true’, help=‘sort by title’)
p.add_argument(‘–rm_blank’, action=‘store_true’, help=‘remove blank’)
p.add_argument(‘–line’, action=‘store’, type=int, default=10, help=‘blank of two problems’)
p.add_argument(‘-r’, ‘–redownload’, action=‘store_true’, help=‘redownload data’)
args = p.parse_args(argv[1:])
return args
def main(argv):
args = handle_args(argv)
x = LeetcodeProblems()
x.args = args
x.run()
if name == ‘main’:
argv = sys.argv
main(argv)
7.将 Markdown 转换为 HTML。
import sys
import os
from bs4 import BeautifulSoup
import markdown
class MarkdownToHtml:
headTag = ‘’
def init(self,cssFilePath = None):
if cssFilePath != None:
self.genStyle(cssFilePath)
def genStyle(self,cssFilePath):
with open(cssFilePath,‘r’) as f:
cssString = f.read()
self.headTag = self.headTag[:-7] + ‘’.format(cssString) + self.headTag[-7:]
def markdownToHtml(self, sourceFilePath, destinationDirectory = None, outputFileName = None):
if not destinationDirectory:
未定义输出目录则将源文件目录(注意要转换为绝对路径)作为输出目录
destinationDirectory = os.path.dirname(os.path.abspath(sourceFilePath))
if not outputFileName:
未定义输出文件名则沿用输入文件名
outputFileName = os.path.splitext(os.path.basename(sourceFilePath))[0] + ‘.html’
if destinationDirectory[-1] != ‘/’:
destinationDirectory += ‘/’
with open(sourceFilePath,‘r’, encoding=‘utf8’) as f:
markdownText = f.read()
编译出原始 HTML 文本
rawHtml = self.headTag + markdown.markdown(markdownText,output_format=‘html5’)
格式化 HTML 文本为可读性更强的格式
beautifyHtml = BeautifulSoup(rawHtml,‘html5lib’).prettify()
with open(destinationDirectory + outputFileName, ‘w’, encoding=‘utf8’) as f:
f.write(beautifyHtml)
if name == “main”:
mth = MarkdownToHtml()
做一个命令行参数列表的浅拷贝,不包含脚本文件名
argv = sys.argv[1:]
目前列表 argv 可能包含源文件路径之外的元素(即选项信息)
程序最后遍历列表 argv 进行编译 markdown 时,列表中的元素必须全部是源文件路径
outputDirectory = None
if ‘-s’ in argv:
cssArgIndex = argv.index(‘-s’) +1
cssFilePath = argv[cssArgIndex]
检测样式表文件路径是否有效
if not os.path.isfile(cssFilePath):
print('Invalid Path: '+cssFilePath)
sys.exit()
mth.genStyle(cssFilePath)
pop 顺序不能随意变化
argv.pop(cssArgIndex)
argv.pop(cssArgIndex-1)
if ‘-o’ in argv:
dirArgIndex = argv.index(‘-o’) +1
outputDirectory = argv[dirArgIndex]
检测输出目录是否有效
if not os.path.isdir(outputDirectory):
print('Invalid Directory: ’ + outputDirectory)
sys.exit()
pop 顺序不能随意变化
argv.pop(dirArgIndex)
argv.pop(dirArgIndex-1)
至此,列表 argv 中的元素均是源文件路径
遍历所有源文件路径
for filePath in argv:
判断文件路径是否有效
if os.path.isfile(filePath):
mth.markdownToHtml(filePath, outputDirectory)
else:
print('Invalid Path: ’ + filePath)
8.文本文件编码检测与转换。
import sys
import os
import argparse
from chardet.universaldetector import UniversalDetector
parser = argparse.ArgumentParser(description = ‘文本文件编码检测与转换’)
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注Python)
漫长,而且极易碰到天花板技术停滞不前!**
因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-CccsGzLB-1712863921176)]
[外链图片转存中…(img-O4PgUgcr-1712863921177)]
[外链图片转存中…(img-dFU8y3vN-1712863921177)]
[外链图片转存中…(img-0dt34FuU-1712863921177)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注Python)