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)+" " 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)+" " 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: -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