Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试创建文件时出现文件名错误_Python_File_Jupyter Notebook - Fatal编程技术网

Python 尝试创建文件时出现文件名错误

Python 尝试创建文件时出现文件名错误,python,file,jupyter-notebook,Python,File,Jupyter Notebook,我正在尝试用以下代码创建一个文件 testnum= '01' file_name = 'output\test'+ testnum +'.txt' with open(file_name,'w',encoding='utf-8') as file: file.write('Hallo') 但我收到了以下错误消息: --------------------------------------------------------------------------- OSError回溯(最

我正在尝试用以下代码创建一个文件

testnum= '01'
file_name = 'output\test'+ testnum +'.txt'
with open(file_name,'w',encoding='utf-8') as file:
    file.write('Hallo')
但我收到了以下错误消息:

---------------------------------------------------------------------------
OSError回溯(最近一次调用上次)
在()
1 testnum='01'
2文件名='output\test'+testnum+'.txt'
---->3打开(文件名,'w')作为文件:
4文件。写入('Hallo')
OSError:[Errno 22]无效参数:“output\test01.txt”

我正在使用Jupyter笔记本。

\t
是制表符。您需要通过加倍来转义
\
,以获得实际的
\
文本:

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

使用
r'output\test'
'output\\test'
。反斜杠是转义字符,因此
'\t'
表示制表符,这不是您在文件名中的意思。非常感谢,它现在起作用了