简体   繁体   English

语法错误:扫描字符串文字时 EOL,不创建并写入文件

[英]SyntaxError: EOL while scanning string literal, Does not Create and Write to File

I am trying to create a file in APPDATA\\Roaming Directory and then write some text to the File.我正在尝试在 APPDATA\\Roaming 目录中创建一个文件,然后向该文件中写入一些文本。 Its a Text file and hence does not work as its supposed to它是一个文本文件,因此无法正常工作

I am getting This as Error我收到这是错误

C:\pytut>python appdatapath.py
  File "appdatapath.py", line 4
    file = path + "\" + 'keys.txt'
                                 ^
SyntaxError: EOL while scanning string literal

what can I say is possibly wrong?我能说什么可能是错的? My codes Looks like this below我的代码如下所示

import os

path = os.getenv('APPDATA')
file = path + "\" + 'keys.txt'

with open(file,'w') as f:
     data = 'Hello Jasmine!'
     f.write(data)
print('Done!')

Kindly assist where I got it wrong.请帮助我出错的地方。 Started Python some few days ago.几天前开始使用 Python。

You better use os.path.join() function你最好使用os.path.join()函数

import os.path
path = os.getenv('APPDATA')
file = os.path.join(path,'keys.txt')

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

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