2017-01-14 50 views
0

我试图创建一个具有以下代码文件名错误当试图创建一个文件

testnum= '01' 
file_name = 'output\test'+ testnum +'.txt' 
with open(file_name,'w',encoding='utf-8') as file: 
    file.write('Hallo') 

一个文件,但我得到了以下错误信息:我使用Jupyter笔记本

--------------------------------------------------------------------------- 
OSError         Traceback (most recent call last) 
<ipython-input-15-322cc04151aa> in <module>() 
     1 testnum= '01' 
     2 file_name = 'output\test'+ testnum +'.txt' 
----> 3 with open(file_name,'w') as file: 
     4  file.write('Hallo') 

OSError: [Errno 22] Invalid argument: 'output\test01.txt' 

+4

使用'r'output \ test''或''产量\\ test''。反斜杠是一个转义字符,所以''\ t''表示一个TAB字符,这不是你在文件名中的含义 – inspectorG4dget

回答

1

\t是制表符。你需要它,以获得实际\文字加倍逃脱\

testnum= '01' 
file_name = 'output\\test'+ testnum +'.txt' 
# Here -------------^ 
+0

非常感谢,它现在可以工作了 –

相关问题