import random
# 生成公司所有員工的資訊,放在一個列表裡面,隨機選擇一個員工
user_list = []
for item in range(1,301):
user_list.append(f"工號-{item}")
lucky_user = random.choice(user_list)
print(lucky_user)
import random
user_list = []
for item in range(1, 301):
user_list.append(f"工號-{item}")
lucky_user_list = random.sample(user_list, 3)
print(lucky_user_list)
import random
user_list = []
with open('userinfo.txt', mode ='r', encodings='utf-8') as f:
for line in f:
line = line.split()[0]
if line:
user_list.append(line)
lucky_user_list = random.sample(user_list, 3)
print(lucky_user_list)
# 案例4、檢視資料夾下面的檔案中的所有使用者
import os
import random
user_list = []
for file_name in os.listdir('files'):
# 獲取部門名字
depart_name = file_name.split(".")[0]
file_path = os.path.join("files", file_name)
f = open(file_path, mode='r', encoding='utf-8')
for line in f:
line = line.split()
if line:
user_list.append(f'{depart_name}-{line}')
f.close()
print(user_list)
lucky_user_list = random.sample(user_list, 3)
print(lucky_user_list)