从文件中获取信息

时间:2014-06-12 21:08:40

标签: python python-2.7 python-2.6

这是我的代码;

Import os
#hard code the path to the external file
external_file = 'names.txt'
username = 'testuser'# Our hardcoded name

#if file doesn't exists, create a new file
if not os.path.exists(external_file):
    #Ask the user's name
    name = raw_input("What's your name?")
    with open(external_file, "a") as f: # using "a" will append to the file
        f.write(name)# Write the name to names.txt
        f.write("\n")
        f.close()
else:
    #if file exists, use it to load name, else ask user
    with open(external_file, "r+") as f:# r+ open a file for reading & writing
        lines = f.read().split('\n') # split the names 
        print lines
        if username in lines: #Check if the file has any username as 'testuser'
            print "Hi {}".format(username)
        else: # If there is no username as 'testuser' then ask for a name
            name = raw_input("What's your name?")
            f.seek(0,2) # Resolves an issue in Windows
            f.write(name)# Write the name to names.txt
            f.write("\n")
            f.close()

这是一个很好的工作代码,但我不希望它显示一个名单列表,并要求你选择。我希望它只存储一个名称,并在每次打开程序时绘制该名称,除非你另有说明 E.g;

IsName = raw_input("That is your name, right?")
If 'no' in IsName:

然后更改文件名称的代码

1 个答案:

答案 0 :(得分:1)

我已使用评论修改了您的代码。这是你想要的吗? 其工作原理如下:

  1. 代码首先检查是否有名为names.txt的文件,如果没有这样的文件,它将创建一个新文件names.txt,然后询问名称。然后它会将此名称保存到文件names.txt

  2. 如果文件names.txt已经存在,那么它将打开该文件并检查它是否包含我们的testuser硬编码名称。如果它包含testuser,那么它会说:

    嗨testuser 这是你的名字吗?

  3. 如果用户输入no,则会询问新名称并将其写入names.txt文件。如果names.txt文件存在并且它不包含硬编码名称testuser,则程序将询问名称并将其写入names.txt文件。

    <强>代码

    import os
    #hard code the path to the external file
    external_file = 'names.txt'
    username = 'testuser'#Our hardcoded name
    
    #if file doesn' exists, create a new file
    if not os.path.exists(external_file):
        #Ask the user's name
        name = raw_input("What's your name?")
        with open(external_file, "a") as f: #using "a" will append to the file
            f.write(name)# Write the name to names.txt
            f.write("\n")
            f.close()
    else:
        #if file exists, use it to load name, else ask user
        with open(external_file, "r") as f:#r+ opens a file for reading & writing
            lines = f.read().split('\n') # split the names 
            f.close()
        if username in lines: #Check if the file has any username as 'testuser'
            print "Hi {}".format(username)
            response = raw_input("Is this your name?")
            if 'no' in response.lower():
                name = raw_input("What's your name?")# Asks for new name
                with open(external_file, "w") as f:
                    f.write(name)# Write the name to names.txt
                    f.write("\n")
                    f.close()
        else: # If there is no username as 'testuser' then ask for a name
            name = raw_input("What's your name?")
            with open(external_file, "w") as f:# Write name to the file
                #f.seek(0,2) # Resolves an issue in Windows
                f.write(name)# Write the name to names.txt
                f.write("\n")
                f.close()
    

    修改后的代码

    import os
    #hard code the path to the external file
    external_file = 'names.txt'
    
    #if file doesn' exists, create a new file
    if not os.path.exists(external_file):
        #Ask the user's name
        name = raw_input("What's your name?")
        with open(external_file, "a") as f: #using "a" will append to the file
            f.write(name)# Write the name to names.txt
            f.write("\n")
            f.close()
    else:
        #if file exists, use it to load name, else ask user
        with open(external_file, "r") as f:#r+ opens a file for reading & writing
            lines = f.read().split('\n') # split the names 
            f.close()
            print "Hi {}".format(lines[0])
            response = raw_input("Is this your name?")
            if 'no' in response.lower():
                name = raw_input("What's your name?") # Asks for new name
                with open(external_file, "w") as f:
                    f.write(name)# Write the name to names.txt
                    f.write("\n")
                    f.close()