从文件中读取行

Geo*_*ows -1 python import text function

我正在编写一个程序来读取5个文件中的文本行,并将这5个文件中的文本编译成相应的列表.

但是我在使程序实际读取文本到列表时遇到了很多麻烦,这是我的代码到目前为止:

from random import random

from dice import choose

b = open ('E:\Videos, TV etc\Python\ca2\beginning.txt', 'r'). readlines ()
stripped_b = [item.strip() for item in b]

a = open ('E:\Videos, TV etc\Python\ca2\adjective.txt', 'r'). readlines ()
stripped_a = [item.strip() for item in a]

i = open ('E:\Videos, TV etc\Python\ca2\inflate.txt', 'r'). readlines ()
stripped_i = [item.strip() for item in i]

n = open ('E:\Videos, TV etc\Python\ca2\noun.txt', 'r'). readlines ()
stripped_n = [item.strip() for item in n]

phrase = []

turn = 0

def business_phrase(x):

    x = raw_input("\n\nInsert a number of business phrases to generate: ")
    while turn <= x:
        turn += 1
        for item in stripped_b:
            random_word = choose(item)
            phrase.append(random_word)
        for item in stripped_a:
            random_word = choose(item)
            phrase.append(random_word)
        for item in stripped_i:
            random_word = choose(item)
            phrase.append(random_word)
        for item in stripped_n:
            random_word = choose(item)
            phrase.append(random_word)
    print random_list
Run Code Online (Sandbox Code Playgroud)

business_phrase(x)的

其中开头,形容词,膨胀和名词是文本文件,骰子是包含选择功能的python文件.

我运行此程序尝试生成一个短语,我收到以下错误消息:

IOError: [Errno 22] invalid mode ('r') or filename 'E:\\Videos, Tv etc\\Python\\ca2\\x08eginning.txt'
Run Code Online (Sandbox Code Playgroud)

我不知道为什么它不会读取文本文件,因为它们与所述的目录相同(实际上与包含该函数的程序位于同一目录中).

有没有人有任何想法我完全被难倒.

Fre*_*Foo 8

处理Windows路径名时,始终使用原始字符串文字:

r'E:\Videos, TV etc\Python\ca2\beginning.txt'
Run Code Online (Sandbox Code Playgroud)

因为否则反斜杠可能被解释为开始转义序列.

另外,请注意字符串末尾的反斜杠:r'C:\'不是您认为的那样.