#encoding=utf-8

from urllib.request import urlopen
import random
import os '''
1. 定义一个fuc(url, folder_path)
获取url地址的内容,保存到folder_path的文件目录下,并随机生成一个文件名。
'''
def save_url_content(url,folder_path=None):
if not (url.startswith('http://') or url.startswith('https://') ):
return u'url地址不符合规格'
if not os.path.isdir(folder_path):
return u'folder_path非文件夹'
d = urlopen(url)
content = d.read()
rand_filename = 'test_%s'%random.randint(1,1000)
file_path = os.path.join(folder_path,rand_filename)
d = open(file_path,'wb')
d.write(content)
d.close()
return file_path print (save_url_content('http://www.baidu.com','tmp')) '''
3. 定义一个func(url),分析该url内容里有多少个链接。
'''
def get_url_count(url):
if not (url.startswith('http://') or url.startswith('https://') ):
return u'url地址不符合规格'
d = urllib.urlopen(url)
content = d.read()
return len(content.split('<a href=')) - 1 # str 到bytes 而精致没解决
print (get_url_count('http://hi.baidu.com/jxq61/item/149d29cc8d52513d4594168f')) '''
2. 定义一个func(folder_path),合并该目录下的所有文件,生成一个all.txt。
'''
def merge(folder_path): if not os.path.exists(folder_path):
return 'not exists' for f in os.listdir(folder_path):
file_path = os.path.join(folder_path,f)
if os.path.isdir(file_path):
merge(file_path)
else:
merge_file = open('merge_test','ab+')
content = open(file_path,'rb').read()
merge_file.write(content)
merge_file.close() merge('tmp') '''
4. 定义一个func(url), 获取他?后的参数,并返回成一个dict。
''' import urlparse
# urlparse模块主要是把url拆分为6部分,并返回元组。
# urllib.parse.urlparse(urlstring, scheme='', allow_fragments=True)
def qs(url):
query = urlparse.urlparse(url).query
return dict([(k,v[0]) for k,v in urlparse.parse_qs(query).items()]) print (qs('http://126.com'))
print (qs('http://api/api?f=5&g=6&y=5'))
print (qs('http://api/api?11=53'))
'''
5. 定义一个func(folder),删除该folder下的所有文件。50421
'''
#使用递归去解决
def delete(folder_path):##习题5
if not os.path.exists(folder_path):
return 'not exists' for f in os.listdir(folder_path):
file_path = os.path.join(folder_path,f)
if os.path.isdir(file_path):
delete(file_path)
else:
os.remove(file_path) delete('/tmp/5')

Python 函数习题的更多相关文章

  1. Pass算子python 函数

    Pass算子python 函数 函数 函数是代码的一种组织形式 函数应该能完成一项特定的工作,而且一般一个函数只完成一项工作 有些语言,分函数和过程两个概念,通俗解释是,有返回结果的是函数,无返回结果 ...

  2. python 函数之day3

    一 函数的语法及特性 什么是函数? 定义:函数是一个功能通过一组语句的集合,由名字(函数名)将其封装起来的代码块,要想执行这个函数,只要调用其函数名即可. 特性: 减少重复代码 使程序变的可扩展 使程 ...

  3. Python函数作用域的查找顺序

    函数作用域的LEGB顺序 1.什么是LEGB? L:local 函数内部作用域 E:enclosing 函数内部与内嵌函数之间 G:global 全局作用域 B:build-in 内置作用域 2.它们 ...

  4. Python函数讲解

    Python函数

  5. Python函数信息

    Python函数func的信息可以通过func.func_*和func.func_code来获取 一.先看看它们的应用吧: 1.获取原函数名称: 1 >>> def yes():pa ...

  6. Python函数参数默认值的陷阱和原理深究"

    本文将介绍使用mutable对象作为Python函数参数默认值潜在的危害,以及其实现原理和设计目的 本博客已经迁移至: http://cenalulu.github.io/ 本篇博文已经迁移,阅读全文 ...

  7. Python开发【第四章】:Python函数剖析

    一.Python函数剖析 1.函数的调用顺序 #!/usr/bin/env python # -*- coding:utf-8 -*- #-Author-Lian #函数错误的调用方式 def fun ...

  8. Python函数解析

    对于Python的函数,我们需要记住的是: 1. 函数的默认返回值是None. 2. python是一个自上而下逐行解释并执行的语言.因此,函数的定义必须在函数被调用之前.同名的函数,后定义的会覆盖前 ...

  9. Python入门笔记(18):Python函数(1):基础部分

    一.什么是函数.方法.过程 推荐阅读:http://www.cnblogs.com/snandy/archive/2011/08/29/2153871.html 一般程序设计语言包含两种基本的抽象:过 ...

随机推荐

  1. spring mvc静态资源访问的配置

    如果我们使用spring mvc来做web访问请求的控制转发,那么默认所有访问都将被DispatcherServlet独裁统治.比如我现在想访问的欢迎页index.html根本无需任何业务逻辑处理,仅 ...

  2. EXCEL类型库的添加

    1. 创建新的C++工程 创建基于对话框的MFC程序 2. 添加库.添加Excel类库 在工程名上右键,选择“添加”—“类”(或者点击菜单栏的“项目”->“添加类”),选择“TypeLib中的M ...

  3. C++ 中的 new/delete 和 new[]/delete[]

    在 C++ 中,你也许经常使用 new 和 delete 来动态申请和释放内存,但你可曾想过以下问题呢? new 和 delete 是函数吗? new [] 和 delete [] 又是什么?什么时候 ...

  4. 在rebar发布的项目中添加监视工具

    默认使用rebar创建的项目没法使用observer,可以如下操作 修改app.src {application, tcp_server, [ {description, ""}, ...

  5. What’s that ALUA exactly?

    What’s that ALUA exactly? 29 September, 20098 Comments Of course by now we have all read the excelle ...

  6. java代码初学者适用,输入学生成绩,符合要求的过~~~~注意数据范围

    总结:没有基础,我从点滴开始, package com.aaa; import java.util.Scanner; //输入“repate ”次数,输入学生成绩,低于60分,输出fail.否则输入p ...

  7. 分布式缓存系统 Memcached 半同步/半异步模式

    在前面工作线程初始化的分析中讲到Memcached采用典型的Master_Worker模式,也即半同步/半异步的高效网络并发模式.其中主线程(异步线程)负责接收客户端连接,然后分发给工作线程,具体由工 ...

  8. [Kingdom Rush]团队分享:如何做塔防手游

    转自:http://www.gamelook.com.cn/2015/03/207324 GameLook报道/2014年11月,乌拉圭开发商Ironhide Studios发布的<Kingdo ...

  9. C#获取程序代码执行时长

    ArrayList list = new ArrayList(); long startTicks = DateTime.Now.Ticks; for (int i = 0; i < 10000 ...

  10. git 批量删除本地分支

    git branch | grep 'bug' |xargs git branch -D