Python 基于计算机存储输入,并根据命令更改存储的输入

Python 基于计算机存储输入,并根据命令更改存储的输入,python,python-2.7,python-2.6,python-idle,Python,Python 2.7,Python 2.6,Python Idle,固定码 if os.path.isfile(external_file): with open(external_file) as in_f: name = in_f.read() else: name = raw_input("What's your name?") with open(external_file, "w") as out_f: out_f.write(name) 问题是 它指的是使用它的每台计算机,其名称存储在.txt

固定码

if os.path.isfile(external_file):
    with open(external_file) as in_f:
        name = in_f.read()
else:
    name = raw_input("What's your name?")
    with open(external_file, "w") as out_f:
        out_f.write(name)
问题是

它指的是使用它的每台计算机,其名称存储在.txt中。 我需要为每个mac地址/ip/计算机使用不同的.txt

我还需要在用户发出命令时更改名称


此外,如果.txt中没有名称,它也不会询问名称?

您可以尝试这样做,首先简单询问用户名,然后检查文件
names.txt
是否存在,如果不存在,则使用name.txt创建一个新文件,并将用户名附加到该文件中。如果文件现在存在,请检查它是否包含用户名,如果包含用户名,请说“Hi+name”,否则将名称附加到文件中

下面是一个快速而肮脏的代码修复程序(可以进一步改进!):

更新:修改版本以仅检查harcoded名称:

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+ 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()

使用
file.seek()
的原因是。

能否请您进一步说明您面临的问题?您所说的
是什么意思?当代码作为聊天机器人联机时,会出现问题,并为每个用户使用一个.txt文件,因此会以告知的名字呼叫每个用户。
还请修复代码的标识@用户3735393您的代码中有些东西毫无意义……例如,如果
外部_文件
不是文件(您可能应该使用
os.path.exists
,而不是
os.path.isfile
),您尝试打开它(可能会失败,因为文件可能不存在),然后什么也不做(
pass
)。这有什么意义?您的示例末尾还有一个
else
块,没有任何相应的
if
,我猜这是一个缩进问题。@dano,如果文件不存在,它怎么会失败?@dano我重新编写了question@Padric坎宁安:你能回答吗?每次都会问这个名字,即使这个名字在.txt中也一样?是的,即使名称在文件中,它也会请求名称,但如果名称已在文件中,它不会将其附加到文件中。它询问名称,因为它必须搜索文件中是否存在给定名称。如果您不提供名称,那么代码如何知道要显示什么名称?我如何使其不要求名称(如果名称在.txt中)?我很抱歉询问,但它在哪里将用户名写入.txt?抱歉,我误解了您的评论。好的,你问了
我该怎么做,这样如果名称在.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+ 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()