如何从计算机访问文本文件?

时间:2015-04-02 19:29:16

标签: list file python-3.x text

我目前的代码如下,但我无法弄清楚如何访问文本文件(例如" john.txt")

    def read_script():
        while True:
            try:
                filename = input('Please Enter Text Name: ')
                F = open (filename, 'r')
                script - F.read()
                F.close()
                slist = script.split()
                return slist
            except OSError:
                print ('Oops! That file does not exist! Try spelling it correctly: ')

    def pig_english():
        letterlist = [i + i[0] for i in read_script()]
        ayList = [i + 'ay' for i in letterlist]
        delaylist = [i[1:] for i in aylist]
        print (delaylist)


    read_script()
    pig_english()

1 个答案:

答案 0 :(得分:0)

你想要raw_input()而不是input()。 input()将用户输入解释为对象而不是字符串。