1.Python实现文件剪切
Python代码实现将某一个路径下的某种格式文件 剪切到其他路径下
import os
def shear():
start_path = input('请输入你要剪切文件的文件路径\n>>>')
goal_path = input('请输入你要剪切到的文件路径\n>>>')
file_hz = input('请输入要过滤的文件格式(例如mp3)\n>>>')
filenames = os.listdir(start_path)
for filename in filenames:
if os.path.splitext(filename)[-1] != ('.'+file_hz):
continue
start_path_filename = os.path.join(start_path, filename)
goal_path_filename = os.path.join(goal_path, filename)
with open(start_path_filename, 'rb') as file1:
file_b = file1.read()
print(f'{start_path_filename}读取成功')
with open(goal_path_filename, 'wb') as file2:
file2.write(file_b)
print(f'{goal_path_filename}存写成功')
os.remove(start_path_filename)
print(f'{start_path_filename}剪切成功')
print('剪切完毕')
if __name__ == '__main__':
shear()
2.Python实现邮编查询地址的操作
读取 youbian.txt 文件中的数据,使用二分查找, 完成邮编查询的操作
def load_zip_code():
with open(r'youbian.txt', 'r', encoding='utf-8') as file:
load_zips_list = eval(file.read())
return load_zips_list
def half_search(data_list, zip_code):
data_list_len = len(data_list)
halve_v = data_list[data_list_len // 2][0]
if halve_v < zip_code:
zip_code_address = half_search(data_list[data_list_len // 2 + 1:], zip_code)
elif halve_v > zip_code:
zip_code_address = half_search(data_list[:data_list_len // 2], zip_code)
elif halve_v == zip_code:
zip_code_address = data_list[data_list_len // 2][1]
else:
zip_code_address = '抱歉,没有这个编码对应的地址'
return zip_code_address
def zip_code_query():
print('*' * 25, '邮编查询器', '*' * 25)
while 1:
try:
zip_code = int(input("输入要查询的邮编号(110100-659001)\n>>>"))
if not (110100 <= zip_code <= 659001):
print("输入的邮编号超出范围")
continue
data_list = load_zip_code()
result = half_search(data_list, zip_code)
print(result, '\n')
except Exception as e:
print("输入的邮编格式有问题", e)
if __name__ == '__main__':
zip_code_query()
3.Python实现逆序储存
例如将一个英文语句以单词为单位逆序排放到指定的文件中
def fback_preser():
sentence = input("请输入一个英文句子:\n>>>")
st_path = input("请输入保存路径\n>>>")
word_list = sentence.split()[::-1]
fb_sentence = ' '.join(word_list)
with open(st_path, 'w', encoding='utf-8') as file:
file.write(fb_sentence)
print("逆序排放写入到指定的文件成功")
if __name__ == '__main__':
fback_preser()
4. Python实现开房查询
输入名字,查询其文件中的开房记录,
如果没有,是一个单纯孩子,
如果有的话,将其所有开房信息写入到以这哥们命名的文件中
def check_open_room():
with open(r'.\kaifanglist.txt', 'r', encoding='utf-8') as file:
open_room_list = file.readlines()
name_list = [open_room.split(',')[0] for open_room in open_room_list]
while 1:
name = input("请输入您要查询的名字:(无输入回车退出)\n>>>")
if name:
if name in name_list:
n = name_list.index(name)
open_room_record = open_room_list[n]
with open(f'{name}.txt', 'w', encoding='utf-8') as file:
file.write(open_room_record)
print(f'{name}的信息已经打印完成啦')
else:
print(f'{name}还是一个单纯的孩子, 没有开房记录. ')
else:
break
if __name__ == '__main__':
check_open_room()
5. Python实现数据持久化
对学生管理系统(或者名片管理系统)进行数据持久化改进
import random
import os
import json
FILE_PATH = './students.json'
def get_all_sids():
return [stu_dict.get('id') for stu_dict in students]
def get_sid():
sid = random.randint(100000, 999999)
while sid in get_all_sids():
sid = random.randint(100000, 999999)
return sid
'''
100000 - 999999 之间随机生成一个 作为学号
'''
def register():
sid = get_sid()
name = input('姓名:')
psw = '0' * 6
age = int(input('年龄:'))
gender = input('性别:')
subject = input('专业:')
stu_dict = dict(id=sid, name=name, psw=psw, age=age, gender=gender, subject=subject)
students.append(stu_dict)
print(f'注册成功, 学号是{sid}, 初始密码是{"0" * 6}')
return stu_dict
def login():
while 1:
try:
sid = int(input('学号:'))
except Exception as e:
print("学号输入错误, 错误码:", e)
continue
break
if sid not in get_all_sids():
print('该学号不存在, 学号输入错误')
else:
psw = input('密码:')
for stu_dict in students:
if stu_dict.get('id') == sid:
if stu_dict.get('psw') == psw:
print('登录成功')
return stu_dict
else:
print('密码错误')
break
return None
def update_psw():
sid = int(input('学号:'))
if sid not in get_all_sids():
print('该学号不存在, 学号输入错误')
else:
psw = input('新密码:')
again_psw = input('确认新密码:')
while psw != again_psw:
print('两次密码不一致 请重新输入')
psw = input('密码:')
again_psw = input('确认密码:')
else:
print('密码修改成功')
for stu_dict in students:
if stu_dict.get('id') == sid:
stu_dict['psw'] = psw
break
def delete():
res = login()
if res:
students.remove(res)
print('注销成功')
def save_user(student_list):
with open(FILE_PATH, 'w', encoding='utf-8') as file:
json.dump(student_list, file, ensure_ascii=False)
def load_users():
global students
if not os.path.exists(FILE_PATH) or os.path.getsize(FILE_PATH) == 0:
students = []
else:
with open(FILE_PATH, 'r', encoding='utf-8') as file:
students = json.load(file)
if __name__ == '__main__':
students = []
load_users()
print('---------欢迎来到学生管理系统---------------')
while True:
print('''系统操作如下:
1.注册 2.登录
3.修改密码 4.注销
0.退出系统
''')
code = input('请输入操作的编号:')
if code == '0':
save_user(students)
break
elif code == '1':
register()
elif code == '2':
login()
elif code == '3':
update_psw()
elif code == '4':
delete()
save_user(students)
|