[英]Python direct path file printing issue?
So I have a script that needs to print to a file in a different directory. 因此,我有一个脚本需要打印到其他目录中的文件。 I give that absolute path and python doesn't like it.
我给出了绝对路径,而python不喜欢它。
Here's where the file is located: C:\\Users\\Owner\\Documents\\Senior_design\\QT_Library\\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\\numbers.txt
文件所在的位置:
C:\\Users\\Owner\\Documents\\Senior_design\\QT_Library\\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\\numbers.txt
(I know, long path, but QT plotter makes the file names really long) (我知道,路径很长,但是QT绘图仪会使文件名非常长)
I typed: 我输入:
textfile = open('C:\Users\Owner\Documents\Senior_design\QT_Library\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt', 'w')
And I get this error: 我得到这个错误:
IOError: [Errno 22] invalid mode ('w') or filename:
I've read that I can use relative paths, but I am unsure how to give it a relative path with so many directories to go through. 我读过我可以使用相对路径,但是我不确定如何给它提供一个相对路径,其中包含要遍历的目录。
Thanks! 谢谢!
The problem is that python is interpreting the backslashes in your path as escape sequences: 问题在于python将路径中的反斜杠解释为转义序列:
>>> 'C:\Users\Owner\Documents\Senior_design\QT_Library\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt'
'C:\\Users\\Owner\\Documents\\Senior_design\\QT_Library\x08uild-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt'
Notice that both \\b
and \\n
get translated to something else. 注意,
\\b
和\\n
被翻译为其他内容。 Use a "raw" string instead: 使用“原始”字符串代替:
>>> r'C:\Users\Owner\Documents\Senior_design\QT_Library\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt'
'C:\\Users\\Owner\\Documents\\Senior_design\\QT_Library\\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\\numbers.txt'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.