繁体   English   中英

扫描字符串文字时的Python EOL

[英]Python EOL while scanning string literal

我正在尝试创建要在类项目中使用的输出文件,但在扫描此行上的字符串文字时会一直给出EOL: outfile = open(r'C:\\Users\\kay\\Documents\\CCA Classes\\CIS 119\\numbers.txt', 'w') 我知道“ \\”是一个特殊字符,但是我的书说r前缀使它成为一个原始字符串,应将其作为文件位置。 我究竟做错了什么?

#This program creates the number file 
import os
def main():
     #Get how many numbers from the user
     many_num = int(input("Enter how many numbers ' + \
                            you will be adding: '))

     #Open a file for writing
     outfile = open(r'C:\Users\kay\Documents\CCA Classes\CIS 119\numbers.txt', 'w'")

     #Get the numbers and write to the file
     for count in range(1,many-num +1):
          numbers = int(input('Enter a number: ' + \
                               str(count) + ': '))

          #write number to file
          outfile.write(str(numbers) + '\n')

     #Close the file
     outfile.close()
     print('Data written to numbers.txt.')

#Call main function
main()

比较这两行,第二行是正确的(可以使用双引号或单引号,没关系)。 通过使用双引号,您将两个参数作为一个字符串传递,并在其中包含单引号和逗号:

#outfile = open(r"C:\Python33\numbers.txt', 'w'")
outfile = open(r'C:\Python33\numbers.txt', 'w')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM