python怎么在电脑里搜索文件_如何在python上搜索记录中的文件?

我正在写一个程序,学生被写在一个文件,可以搜索时,输入他们的具体数据。不过,我不确定脚本中的search_record方法是否正确。它有时可以返回正确的结果,但有时当输入不同的数据时,它不起作用。在

TA=教师顾问

这是我目前的计划:def read_record():

in_file = open("student.txt", "r")

stud_num = in_file.readline()

while stud_num != "":

last_name = in_file.readline

first_name = in_file.readline

TA = in_file.readline()

stud_num = stud_num.rstrip("\n")

last_name = last_name.rstrip("\n")

first_name = first_name.rstrip("\n")

TA = TA.rstrip("\n")

print "Student number: ", stud_num

print "Last name: ", last_name

print "First name: ", first_name

print "TA: ", TA

print

stud_num = in_file.readline()

in_file.close()

def write_record():

num_students = input("Enter number of students: ")

out_file = open("student.txt" , "w")

for counter in range (1, num_students + 1):

stud_num = raw_input("Enter student number: ")

last_name = raw_input("Enter last name: ")

first_name = raw_input("Enter first name: ")

TA = raw_input("Enter TA: ")

out_file.write(stud_num +"\n")

out_file.write(last_name +"\n")

out_file.write(first_name +"\n")

out_file.write(TA + "\n")

out_file.close()

def menu():

print "STUDENT MENU"

print "============="

print "A. Create student file"

print "B. Display student file"

print "C. Add student file"

print "D. Search records"

print

choice = raw_input("Enter your choice: ")

return choice

def add_record():

out_file = open("student.txt", "a")

another = "Y"

while another == "Y":

stud_num = raw_input("Enter student number: ")

last_name = raw_input("Enter last name: ")

first_name = raw_input("Enter first name: ")

TA = raw_input("Enter TA: ")

print

out_file.write(stud_num + "\n")

out_file.write(last_name + "\n")

out_file.write(first_name + "\n")

out_file.write(TA + "\n")

another = raw_input("Do you want to enter another student (Y or N): ")

out_file.close()

def search_record():

in_file = open("student.txt", "r")

found = False

print "RECORD MENU"

print "============="

print "A. Search student number"

print "B. Search student last name"

print "C. Search student name"

print "D. Search student TA"

print

choice = raw_input("Enter your choice: ")

if choice == "A":

search = raw_input("Enter a student number: ")

stud_num = in_file.readline()

while stud_num != "":

last_name = in_file.readline()

first_name = in_file.readline()

TA = in_file.readline()

stud_num = stud_num.rstrip("\n")

last_name = last_name.rstrip("\n")

first_name = first_name.rstrip("\n")

TA = TA.rstrip("\n")

if search == stud_num:

print "Student number: ", stud_num

print "Last name: ", last_name

print "First name: ", first_name

print "TA: ", TA

print

found = True

stud_num = in_file.readline()

elif choice == "B":

search = raw_input("Enter a student last name: ")

last_name = in_file.readline(2)

while last_name != "":

stud_num = in_file.readline()

first_name = in_file.readline()

TA = in_file.readline()

stud_num = stud_num.rstrip("\n")

last_name = last_name.rstrip("\n")

first_name = first_name.rstrip("\n")

TA = TA.rstrip("\n")

if search == last_name:

print "Student number: ", stud_num

print "Last name: ", last_name

print "First name: ", first_name

print "TA: ", TA

print

found = True

last_name = in_file.readline()

elif choice == "C":

search = raw_input("Enter a student name: ")

first_name = in_file.readline(3)

while first_name != "":

stud_num = in_file.readline()

last_name = in_file.readline()

TA = in_file.readline()

stud_num = stud_num.rstrip("\n")

last_name = last_name.rstrip("\n")

first_name = first_name.rstrip("\n")

TA = TA.rstrip("\n")

first_name = in_file.readline()

if search == first_name:

print "Student number: ", stud_num

print "Last name: ", last_name

print "First name: ", first_name

print "TA: ", TA

print

found = True

first_name = in_file.readline()

elif choice == "D":

search = raw_input("Enter a TA: ")

TA = in_file.readline(4)

while TA != "":

stud_num = in_file.readline()

last_name = in_file.readline()

first_name = in_file.readline()

stud_num = stud_num.rstrip("\n")

last_name = last_name.rstrip("\n")

first_name = first_name.rstrip("\n")

TA = TA.rstrip("\n")

if search == TA:

print "Student number: ", stud_num

print "Last name: ", last_name

print "First name: ", first_name

print "TA: ", TA

print

found = True

TA = in_file.readline()

else:

print "Invalid entry"

in_file.close()

if not found:

print "Student record not found"

def main():

choice = menu()

if choice == "A":

write_record()

elif choice == "B":

read_record()

elif choice == "C":

add_record

elif choice == "D":

search_record()

else:

print "error"

main()

我为测试创建的学生文件是

^{pr2}$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值