Windows 中 os.path.join 出现不可预测的结果
所以我想做的是以
os.path.join('C:\path\to\folder', 'filename').
**edit 的形式加入一些内容: 实际代码是:
filename = 'creepy_%s.pcl' % identifier
file = open(os.path.join(self.cache_dir, filename), 'w')
其中 self.cache_dir 是使用 configobj 从文件中读取的(返回字符串),在特定情况下是 '\Documents and Settings\Administrator\creepy\cache'
第一部分是使用 configobj 从配置文件返回的。第二个是 2 个字符串的串联,例如: 'file%s' % name
当我使用安装的 python 解释器通过 Windows 中的控制台运行应用程序时,我得到了预期的结果,即
C:\\path\\to\\folder\\filename
当我捆绑相同的应用程序和 python 解释器时(相同版本,2.6)在 Windows 中的可执行文件中并运行应用程序,结果是
C:\\path\\to\\folderfilename
任何关于可能是什么问题的线索,或者什么会导致输出中出现这种不一致?
So what I'm trying to do is to join something in the form of
os.path.join('C:\path\to\folder', 'filename').
**edit :
Actual code is :
filename = 'creepy_%s.pcl' % identifier
file = open(os.path.join(self.cache_dir, filename), 'w')
where self.cache_dir is read from a file using configobj (returns string) and in the particular case is '\Documents and Settings\Administrator\creepy\cache'
The first part is returned from a configuration file, using configobj. The second is a concatenation of 2 strings like: 'file%s' % name
When I run the application through the console in windows using the python interpreter installed, I get the expected result which is
C:\\path\\to\\folder\\filename
When I bundle the same application and the python interpreter (same version, 2.6) in an executable in windows and run the app the result is instead
C:\\path\\to\\folderfilename
Any clues as to what might be the problem, or what would cause such inconsistencies in the output ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码格式错误。您需要将这些反斜杠加倍或使用原始字符串。
我不知道为什么它在一种解释器中工作,而在另一种解释器中不起作用,但您的代码将无法按原样正确解释。奇怪的是我期望有不同的输出,即:
C:pathtofolder\filename
。Your code is malformed. You need to double those backslashes or use a raw string.
I don't know why it works in one interpreter and not the other but your code will not be interpreted properly as is. The weird thing is i'd have expected a different output, ie:
C:pathtofolder\filename
.这是令人惊讶的行为。它没有理由这样做。
请小心,您可以将行更改为以下内容。
请注意\
r''
原始字符串和最终的It is surprising behavior. There is no reason it should behave in such a way.
Just be be cautious, you can change the line to the following.
Note the
r''
raw string and the final\
您可以执行以下三件事:
在原始字符串“C:\\path\\to\\folder”中使用双斜杠
使用原始字符串,r'C:\path\to\folder'
使用正斜杠,'C:/path/to/folder'
Three things you can do:
Use double-slashes in your original string, 'C:\\path\\to\\folder'
Use a raw string, r'C:\path\to\folder'
Use forward-slashes, 'C:/path/to/folder'
我昨天就明白了像往常一样,当事情看起来很奇怪时,解释很简单,而且大多数时候都表明你很愚蠢。
长话短说,dist-packages 中还存在一些以前安装的剩余内容。捆绑的解释器从那里加载模块,但是当我从终端运行 python 脚本时,当前目录中的模块(较新版本)被加载。因此出现了“不可预测”的结果。
I figure it out yesterday. As usual when things seem really strange, the explanation is very simple and most of the times involve you being stupid.
To cut a long story short there were leftovers from some previous installations in dist-packages. The bundled interpreter loaded the module from there , but when i ran the python script from the terminal , the module (newer version) in the current dir was loaded. Hence the "unpredictable" results.