1.模块化作业

1.回顾文件递归遍历. 默写一遍.

入口在: 当文件是个文件夹的时候

出口在: 文件是一个文件

2.计算时间差(用户输入起始时间和结束时间. 计算时间差(小时),

例如, 用户输入2018-10-08 12:00:00 2018-10-08 14:30:00 输出2小时

3.写一个函数,接收一个参数,如果是文件,就执行这个文件,如果是文件夹,就执行这个文件夹下的所有的py文件。

4.写一个copy函数,接受两个参数,第一个参数是源文件的位置,第二个参数是目标位置,将源文件copy到目标位置。

5.获取某个文件所在目录的上一级目录。

30分

6.使用os模块创建如下目录结构

# glance
# ├── __init__.py
# ├── api
# │ ├── __init__.py
# │ ├── policy.py
# │ └── versions.py
# ├── cmd
# │ ├── __init__.py
# │ └── manage.py
# └── db
# ├── __init__.py
# └── models.py

7.写一个用户注册登陆的程序,每一个用户的注册都要把用户名和密码用字典的格式写入文件userinfo。在登陆的时候,再从文件中读取信息进行验证。

8.使用random模块,编写一个发红包的函数

2.作业详解

点击查看详细内容

#1.
import os
def func(path,n=0):
path_list = os.listdir(path) #1.打开文件夹,查看当前目录的文件
for name in path_list: #2.遍历文件名
abs_path = os.path.join(path,name) #3.文件绝对路径
if os.path.isdir(abs_path):
print("\t"*n,name,":")
func(abs_path,n+1) #递归,n:层数,多一层多一个 "\t"
else:
print("\t"*n,name) ret = func('E:\S15\day1')

2.

import time

start_time = time.strptime('2018-10-08 12:00:00','%Y-%m-%d %H:%S:%M')

end_time = time.strptime('2018-10-08 14:30:00','%Y-%m-%d %H:%S:%M')

sub_time = time.mktime(end_time) - time.mktime(start_time)

gm_time = time.gmtime(sub_time)

print('过去了%d年%d月%d日%d时%d分%d秒'%(gm_time.tm_year-1970,gm_time.tm_mon-1,

gm_time.tm_mday-1,gm_time.tm_hour,

gm_time.tm_min,gm_time.tm_sec))

3.

思路:

#先判断这个path是文件还是文件夹
#如果是文件:.py结尾的,执行文件:os.system('python path')
#如果是文件夹:查看文件夹下的所有内容,如果是文件.py结尾则执行

import os

def exec_py(path):

if os.path.isfile(path) and path.endswith('.py'):

os.system('python %s'%path)

elif os.path.isdir(path):

path_list = os.listdir(path)

for name in path_list:

abs_path = os.path.join(path,name)

if os.path.isfile(abs_path) and abs_path.endswith('.py'):

os.system('python %s'%abs_path)

elif os.path.isdir(abs_path):

exec_py(abs_path)

exec_py(r'E:\S15\day20')

4.

def copy(path1,path2):

filename = os.path.basename(path1)

if os.path.isfile(path1) and os.path.isdir(path2):

path2 = os.path.join(path2,filename)

if os.path.exists(path2):

print('文件已存在')

else:

with open(path1,'rb') as f1,

open(path2,'wb') as f2:

content = f1.read()

f2.write(content)

copy(r'E:\S15\day20\01.内容回顾.txt',r'E:\S15\day19')

5.

def get_cwd(path):

return os.path.dirname(path)

ret = get_cwd('E:\S15\day20')

6.

os.makedirs('glance/api')

os.makedirs('glance/cmd')

os.makedirs('glance/db')

open('glance/init.py','w').close()

open('glance/api/init.py','w').close()

open('glance/api/policy.py','w').close()

open('glance/api/versions.py','w').close()

open('glance/cmd/init.py','w').close()

open('glance/cmd/manage.py','w').close()

open('glance/db/init.py','w').close()

open('glance/db/models.py.py','w').close()

7.

思路:

import pickle

def enrol():

username = input("Input Username:")

password = input("Input Password:")

dic = {'usr':username,'pwd':password}

with open('userinfo','ab') as f:

pickle.dump(dic,f)

print('注册成功')

enrol()

def login():

username = input("Input Username:")

password = input("Input Password:")

with open('userinfo','rb') as f:

while True:

try:

str_dic = pickle.load(f)

if str_dic['usr'] == username and str_dic['pwd'] == password:

print('登陆成功')

break

except EOFError:

print('登陆失败')

break

login()

8.发红包

import random

def send_money(money,num):

money = money * 100 #整数进两位,后面算出小数

ret = random.sample(range(1,money),num-1)

ret.sort()

ret.insert(0,0)

ret.append(money)

#print(ret)

for i in range(len(ret)-1):

yield (ret[i+1] - ret[i])/100

ret_g = send_money(200,10)

for i in ret_g:

print(i)

day20模块作业的更多相关文章

  1. Python:Day20 模块

    模块是用来组织函数的. 模块一共3种: python标准库 第三方模块 应用程序自定义模块 模块搜索路径:sys.path import sys print(sys.path) import calc ...

  2. day20 模块-sys,time,collection

    所有常用模块的用法:  http://www.cnblogs.com/Eva-J/articles/7228075.html 前情回顾: # 常用模块 # 常用模块 —— 东西多 # 异常处理 # 什 ...

  3. day20 模块 collections time sys os

    1.模块 1. 模块的定义:我们把装有特定功能的代码进行归类的结果.我们目前写的所有py文件都是模块 2. 引入模块的方式: 1.import 模块名 2.from xxx import 模块名 2. ...

  4. 常用模块Part(1)

    collections模块 time模块 random模块 os模块 sys模块 collections模块 这个模块实现了一些很好的数据结构,它们能帮助你解决各种实际问题 在这里主要介绍几种数据结构 ...

  5. python(31)- 模块练习

    1. 小程序:根据用户输入选择可以完成以下功能:     创意文件,如果路径不存在,创建文件夹后再创建文件     能够查看当前路径     在当前目录及其所有子目录下查找文件名包含指定字符串的文件 ...

  6. [ python ] 学习目录大纲

    简易博客[html+css]练习 MySQL 练习题及答案 MySQL视图.触发器.函数.存储过程 MySQL 操作总结 Day41 - 异步IO.协程 Day39/40 - 线程的操作 Day36/ ...

  7. 老男孩Python全栈第2期+课件笔记【高清完整92天整套视频教程】

    点击了解更多Python课程>>> 老男孩Python全栈第2期+课件笔记[高清完整92天整套视频教程] 课程目录 ├─day01-python 全栈开发-基础篇 │ 01 pyth ...

  8. Python学习进程

    1周第1天 主要是变量的学习(11月8日) 1.1 python安装(win和linux下)1.2 ipython安装及使用1.3 变量的定义1.4 变量赋值1.5 运算符(赋值.算术.关系.逻辑)1 ...

  9. Linux - 请允许我静静地后台运行

    h1,h2,h3,h4,h5,h6,p,blockquote { margin: 0; padding: 0 } body { font-family: "Helvetica Neue&qu ...

随机推荐

  1. loj515贪心只能过样例

    bitset练习题... 位运算真的是玄学... 一开始真的“只能过样例” 后来发现把左移写成了小于号 鬼知道我在想什么/手动微笑 loj第一题 #include<iostream> #i ...

  2. 使用IronPython集成Python和.NET

    本文由 伯乐在线 - 艾凌风 翻译,Daetalus 校稿.未经许可,禁止转载!英文出处:www.informit.com.欢迎加入翻译组. 从两个优秀的世界各取所需,更高效的复用代码.想想就醉了,. ...

  3. Asp.net工作流workflow实战之书签(二)

    1.winform(web程序)下使用工作流 怎样才能像控制台那样让winform或web页面窗体阻塞等待工作流的继续执行呢 2.BookMark书签 书签:和一般的书签看书的时候方便查看上次看的内容 ...

  4. Linux CentOS安装Azure Cli工具

    vim /etc/yum.repos.d/epel.repo [epel] name=epel baseurl=http://mirrors.sohu.com/fedora-epel/6/$basea ...

  5. 人物-IT-张志东:张志东

    ylbtech-人物-IT-张志东:张志东 张志东,广东东莞人,腾讯创办人之一,腾讯高级副总裁兼科技总裁,于1993年取得深圳大学理学学士学位,并于1996年取得华南理工大学计算机应用及系统架构硕士学 ...

  6. 用户收到"无法显示页面"的错误消息和"Connections_refused"条目记录在运行 Windows Server 2003,Exchange 2003 和 IIS 6.0 的服务器上的 Httperr.log 文件

    症状 您会遇到下列症状在运行 Microsoft Windows Server 2003. Microsoft Exchange Server 2003年和 Microsoft Internet In ...

  7. 十六、xx.xx.xx格式版本号大小比较

    DELIMITER $$ USE `deshangshidai`$$ DROP FUNCTION IF EXISTS `STRCMP_MY_VERSION`$$ CREATE DEFINER=`roo ...

  8. 转载:oracle用户创建及权限设置

    权限: create session create table unlimited tablespace connect resource dba 例: #sqlplus /nolog SQL> ...

  9. .net关于应用程序缓存的一些疑惑

    疑惑:获取缓存后强制转换为实体对象传递给前台,如果前台对这个实体对象中属性更改的话缓存中的数据也随之改变,为啥??? 首先是创建缓存的方法: /// <summary> /// 创建缓存项 ...

  10. Linux下UDP发送大量请求导致Operation not permitted的问题探讨

    一.问题背景目前公司准备上微服务的架构模式,在对比大量的API网关之后,最后选中了Kong作为我们的API网关,在经过大量的研究之后,啊呸,踩坑之后,终于跑起来了,简直是普天同庆,撒花祝贺. 但是在美 ...