1. 小程序:根据用户输入选择可以完成以下功能:

    创意文件,如果路径不存在,创建文件夹后再创建文件


    能够查看当前路径


    在当前目录及其所有子目录下查找文件名包含指定字符串的文件

import os
choice={
"1":"创建文件",
"2":"查看当前路径",
"3":"查找文件名",
"4":"退出程序"
} def mkdir():
file_path = input("please input your file_path:").strip()
try:
os.chdir(file_path)
print("%s存在,不需要重新创建" % file_path)
except:
print("%s不存在,开始创建文件" % file_path)
os.makedirs(file_path)
print("%s创建完成" % file_path) def check():
file_name = input("please input your file_name:").strip()
try:
print("%s绝对路径为:%s"%(file_name,os.path.abspath(file_name)))
except:
print("%s不存在,开始创建文件" % file_name) def search():
file_dir = input("please input your file_dir:").strip()
print("当前目录为:%s,子目录和所有文件为:%s" % (os.getcwd(), os.listdir(os.getcwd()))) while True:
for key in choice:
print(key,choice[key])
num=int(input("please input your choice:").strip()) if num==1:
mkdir() elif num==2:
check() elif num == 3:
search() elif num==4:
break

  

2. 将三次登陆锁定的作业改为:python login.py -u alex -p 123456 输入的形式(-u,-p是固定的,分别代表用户名和密码)

#将三次登陆锁定的作业改为:
# python login.py -u alex -p 123456 输入的形式
# (-u,-p是固定的,分别代表用户名和密码)
import sys
if(sys.argv[sys.argv.index("-u")+1] == "egon" and sys.argv[sys.argv.index("-p")+1] == "somebody" ):
print("Congratulations")
else:
print("Error")
#!/bin/python3
# -*- coding:utf-8 -*-
''' python login auth example '''
#imports
import getpass,os,sys
#functions
def lock_user(username):
''' username -> modify userlist file '''
temp_str = ""
with open("userlist.swp",'w') as file_write , open("userlist",'r') as file_read:
for line in file_read:
temp_list = eval(line)
if temp_list[0] == username:
temp_list[2] += 1
temp_str += str(temp_list)+"\n"
continue
temp_str += line
file_write.write(temp_str)
os.rename("userlist.swp", "userlist")
def unlock_user(username):
''' username -> modify userlist file '''
temp_str = ""
with open("userlist.swp",'w') as file_write , open("userlist",'r') as file_read:
for line in file_read:
temp_list = eval(line)
if temp_list[0] == username:
temp_list[2] = 0
temp_str += str(temp_list)+"\n"
continue
temp_str += line
file_write.write(temp_str)
os.rename("userlist.swp", "userlist")
def user_input():
''' input username&&password -> return username&&password '''
if not "-u" in sys.argv and not "-p" in sys.argv:
print("help:\n-u [username] -p [password]")
exit()
username = sys.argv[sys.argv.index("-u")+1]
password = sys.argv[sys.argv.index("-p")+1]
# username = input("username:")
# password = getpass.getpass("password:")
return username,password
def check_user(username,password = '',type = 'check'):
'''
username,password,check -> return t||f #username password lock check
username,password,lock -> return t||f #username locked or not
'''
flag = False
with open("userlist",'r') as file_read:
for line in file_read:
temp_list = eval(line)
if type == 'check':
if username == temp_list[0] and password == temp_list[1] \
and temp_list[2] < 2:
return True #user not locked and correct user and pass
if type == 'lock':
if username == temp_list[0] and temp_list[2] >= 2:
return True #user has been locked else:
return False
#login decorator
def auth(func):
def wrapper(*args,**kwargs):
username,password = user_input() #get input
# if not username and not password:
# print("incorrect username or password!")
# exit()
if check_user(username, password,type='check'): #juge username and password correct or not
print("login success!")
unlock_user(username)
func(username) #shell function
else:
if check_user(username, password, type='lock'): #juge user locked or not
print("This account has been locked!")
else:
lock_user(username) #lock count += 1
print("incorrect username or password!")
return wrapper
#shell function
@auth
def login_index(username = ""):
''' shell program '''
while True:
command = input("%s>" %username) #ask input command
if command == 'ls':
print(
'''
bin dev home lib64 media opt root sbin sys usr
boot etc lib lost+found mnt proc run srv tmp var
''')
if command == 'exit': #exit shell
print("byebye!")
exit() def main():
''' main program '''
login_index() #program entry
if __name__ == '__main__':
main()

  

3. 层级结构:


    dir1


    ---hello.py


    dir2


    ---main.py


    其中,hello.py:


    def add(x,y):


        
return x+y

 main.py如何能调用到hello.py中的add函数?

参考博客结尾的内容,单独导入包。http://www.cnblogs.com/xuyaping/p/6797032.html

注:此题中导入的模块只是一个简单的模块,和博客中导入模块不同的是没有__init__文件,更简单些。

#main.py
import sys,os
sys.path.append("E:\python学习\day33常用模块\作业\dir1")
print(sys.path)
--->['E:\\python学习\\day33常用模块\\作业\\dir2', 'E:\\python学习', 'C:\\Users\\Administrator.PC-201509301704\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip', 'C:\\Users\\Administrator.PC-201509301704\\AppData\\Local\\Programs\\Python\\Python36\\DLLs', 'C:\\Users\\Administrator.PC-201509301704\\AppData\\Local\\Programs\\Python\\Python36\\lib', 'C:\\Users\\Administrator.PC-201509301704\\AppData\\Local\\Programs\\Python\\Python36', 'C:\\Users\\Administrator.PC-201509301704\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages', 'E:\\python学习\\day33常用模块\\作业\\dir1'] import hello
print(hello.add(1,2))
--->3

4. 显示当前时间三天后是星期几?

import time
c=time.localtime()
print(c) #当前时间
--->time.struct_time(tm_year=2017, tm_mon=4, tm_mday=26, tm_hour=23, tm_min=40, tm_sec=54, tm_wday=2, tm_yday=116, tm_isdst=0) t=time.time()-3600*24*3
print(time.localtime(t))
--->time.struct_time(tm_year=2017, tm_mon=4, tm_mday=23, tm_hour=23, tm_min=44, tm_sec=30, tm_wday=6, tm_yday=113, tm_isdst=0) print(time.localtime(t).tm_wday)
--->6

  

python(31)- 模块练习的更多相关文章

  1. python基础-模块

    一.模块介绍                                                                                              ...

  2. Day05 - Python 常用模块

    1. 模块简介 模块就是一个保存了 Python 代码的文件.模块能定义函数,类和变量.模块里也能包含可执行的代码. 模块也是 Python 对象,具有随机的名字属性用来绑定或引用. 下例是个简单的模 ...

  3. python 各模块

    01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...

  4. python random模块 - 小驹的专栏 - 博客频道 - CSDN.NET

    python random模块 - 小驹的专栏 - 博客频道 - CSDN.NET python random模块 分类: python 2011-11-15 15:31 6037人阅读 评论(2) ...

  5. Day5 模块及Python常用模块

    模块概述 定义:模块,用一砣代码实现了某类功能的代码集合. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,提供了代码的重用性.在Python中,一个.py文件就称之为一个模块(Mod ...

  6. SUSE Linux Enterprise 11 离线安装 DLIB python机器学习模块

    python机器学习模块安装 环境:SUSE Linux Enterprise 11 sp4  离线安装 说明:在安装dlib时依赖的基础 环境较多,先升级gcc,以适应c++ 11的使用:需要用到c ...

  7. Python(五)模块

    本章内容: 模块介绍 time & datetime random os sys json & picle hashlib XML requests ConfigParser logg ...

  8. Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures

    参考博客: https://www.cnblogs.com/xiao987334176/p/9046028.html 线程简述 什么是线程?线程是cpu调度的最小单位进程是资源分配的最小单位 进程和线 ...

  9. Python之模块和包

    一.模块 1.什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编 ...

  10. python常用模块之时间模块

    python常用模块之时间模块 python全栈开发时间模块 上次的博客link:http://futuretechx.com/python-collections/ 接着上次的继续学习: 时间模块 ...

随机推荐

  1. [svn学习篇]svn使用教程

    http://www.cnblogs.com/longshiyVip/p/4905901.html http://blog.csdn.net/dily3825002/article/details/6 ...

  2. kb-09-线段树--区间合并比较繁

    /* hdu-1540 题意:一个线段,长度为n,三种操作,Dx,挖掉某个点:R,恢复最近被挖掉的点:Qx查询该点所在的连续区间的长度: 树的节点维护三个变量,该节点左边界开始连续的个数ll,右边界开 ...

  3. BZOJ4598 [Sdoi2016]模式字符串 【点分治 + hash】

    题目 给出n个结点的树结构T,其中每一个结点上有一个字符,这里我们所说的字符只考虑大写字母A到Z,再给出长度为m 的模式串s,其中每一位仍然是A到z的大写字母.Alice希望知道,有多少对结点< ...

  4. mybatis学习(十)——缓存介绍

    与Hibernate一样,MyBatis 也提供了一级缓存和二级缓存的支持. 1.一级缓存:(本地缓存)SqlSession级别的缓存,默认一直开启的 , 与数据库同一次会话期间的数据会放到本地缓存中 ...

  5. [USACO12DEC]第一!First! (Trie树,拓扑排序)

    题目链接 Solution 感觉比较巧的题啊... 考虑几点: 可以交换无数次字母表,即字母表可以为任意形态. 对于以其他字符串为前缀的字符串,我们可以直接舍去. 因为此时它所包含的前缀的字典序绝对比 ...

  6. 【bzoj1406】 AHOI2007密码箱 数论

    在一次偶然的情况下,小可可得到了一个密码箱,听说里面藏着一份古代流传下来的藏宝图,只要能破解密码就能打开箱子,而箱子背面刻着的古代图标,就是对密码的提示.经过艰苦的破译,小可可发现,这些图标表示一个数 ...

  7. Linux rpm 命令参数使用

    RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种 ...

  8. @login_required用法简介

    在django项目中,经常会看到下面这样的代码: from django.contrib.auth.decorators import login_required @login_required d ...

  9. Spring Boot的web开发&静态资源配置方式

    Web开发的自动配置类:org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration 1.1. 自动配置的ViewResolve ...

  10. Java后端WebSocket的Tomcat实现 html5 WebSocket 实时聊天

    WebSocket协议被提出,它实现了浏览器与服务器的全双工通信,扩展了浏览器与服务端的通信功能,使服务端也能主动向客户端发送数据.Tomcat7.0.47上才能运行. 需要添加Tomcat里lib目 ...